gpt4 book ai didi

python - 以写入和追加模式打开文件

转载 作者:太空宇宙 更新时间:2023-11-04 01:26:12 25 4
gpt4 key购买 nike

我正在尝试将一些数据附加到如下文件中..但是当文件不存在时这会失败..我尝试了以下操作。任何人都可以提供在两种写入模式下打开文件的输入,以便它在文件不存在时创建文件并同时添加模式吗?

代码:-

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com