我正在尝试将一些数据附加到如下文件中..但是当文件不存在时会失败.. 我尝试了以下操作..任何人都可以提供在两种写入模式下打开文件的输入,以便它在文件不存在时创建文件并同时添加模式吗?
代码:-
c = csv.writer(open("//DATA/loc" + csv_file + ".csv", "ab"))
c.writerow([format_time, icount])
最佳答案
使用
c = csv.writer(open("//DATA/loc" + csv_file + ".csv", "a+b"))
c.writerow([format_time, icount])
而不是
c = csv.writer(open("//DATA/loc" + csv_file + ".csv", "ab"))
c.writerow([format_time, icount])
关于python - 以写入和追加模式打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17756190/