我有一个需要逐一打开的数据库文件列表,并且将Python is a great language.\nYeah its great!!
写入所有文件。if语句后文件没有打开
问题:即使文件存在,我也无法打开文件。 files
包括:
1.txt
2.txt
3.txt
我的代码:
for files in cursor.fetchall():
sfile= files[1]
if os.path.exists(os.path.join(path,sfile)):
with open(sfile,'r') as f:
f.write("Python is a great language.\nYeah its great!!\n");
f.close()
错误:
with open(sfile,'r') as f:
IOError: [Errno 2] No such file or directory: '1.txt'
>>>
请帮我纠正我的错误!
你正在检查'os.path.join(path,sfile)',但是打开'sfile'(在'r'ead模式下)。 – jonrsharpe 2014-10-20 16:28:53