我希望在创建新文件时避免意外替换。你知道吗
我的代码是检查文件名。你知道吗
fname = "myFile"
contents = "abc"
if fname is existing: #How to check this?
print("The file name is already existing, would you want to replace the file?")
myAnswer = input("The answer is : ")
print("Your answer is " + myAnswer)
if myAnswer == "yes":
fid = open(fname,'w')
fid.write(contents)
fid.close()
else:
print("rejected")
else:
fid = open(fname, 'w')
fid.write(contents)
fid.close()
但是,我不知道如何扫描文件夹中的文件名。你知道吗
有人能帮我吗?你知道吗
您可以使用
os
库:如果文件的路径存在或是符号链接,则返回
True
。你知道吗相关问题 更多 >
编程相关推荐