我正在尝试打开一个文件,如果该文件不存在,我需要创建它并打开它以进行写入。到目前为止,我有这样的想法:
#open file for reading
fn = input("Enter file to open: ")
fh = open(fn,'r')
# if file does not exist, create it
if (!fh)
fh = open ( fh, "w")错误消息说if(!fh)线路上有一个问题。我可以像在Perl中一样使用exist吗?
发布于 2020-04-12 06:08:17
下面是一个快速的两行代码,如果文件不存在,我可以用它快速创建一个文件。
if not os.path.exists(filename):
open(filename, 'w').close()https://stackoverflow.com/questions/35807605
复制相似问题