这是我到目前为止的代码,是的,我知道“dirs”代码创建一个文件夹,但我不确定创建文件的代码(如果有的话)!
import os
if os.path.isfile(outfile):
print("Name already used, please choose another")
else:
os.makedirs(outfile)
任何帮助将不胜感激:D
最佳答案
为了创建文件并对其进行操作,请使用 open()
函数。
fp = open(outfile, 模式)
模式:
r:读取
w:如果文件存在,则替换为新文件
a:追加现有文件
在您的情况下,模式是“w”
关于python - 如何在 python 代码中创建文件(如果可用,则默认为文本文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27041772/