【发布时间】:2015-12-08 02:04:59
【问题描述】:
我实际上是在尝试使用 python 脚本处理当前工作目录中的每个文件。尽管我尽了最大努力,但在运行代码时仍会出现此错误:
File "script.py", line 9, in <module>
with my_file as f:
AttributeError: __exit__
这是我的代码:
import os
for my_file in os.listdir(os.getcwd()):
first = True;
movie_id = 0;
with my_file as f:
for line in f:
if first == False:
sys.stdout.write(movie_id + "," + line);
else:
movie_id = line
movie_id = movie_id[0:len(movie_id) - 2]
first = False
有什么建议吗?谢谢!
【问题讨论】:
-
您可能打算在
my_file上致电open()。
标签: python io file-handling