打开文件以及打开行

Aks*_*ari 0 python file-io

我必须从文件中读取一行进行一些计算并写入另一个文件.我做过这样的事情

fd= open("abc.txt","w")
for line in open("test.txt","r"):
     Do something Here
     fd.write(modifiedline)
Run Code Online (Sandbox Code Playgroud)

我通常使用open和for open进行逐行操作.我使用的上述方式是否可以使用,或者是否有更好的方式我们使用open和open一起使用.

我是学生,希望了解更多.任何帮助表示赞赏.

The*_*nse 5

with open("abc.txt", 'w') as outfi,open("test.txt","r") as infil:
    for line in infil:
        Do something Here
        outfi.write(modifiedline)
Run Code Online (Sandbox Code Playgroud)