命令 exists:
文件名字符串作为参数,如果文件存在的话,它将返回 True,否则将返回 False。
# -- coding: utf-8 --from sys import argv # 导入函数或者模块
from os.path import exists(script, from_file, to_file) = argv # 解包print("Copying from %r to %r" % (from_file, to_file))inputfile = open(from_file)
indata = inputfile.read()print("The input file is %d bytes long" % len(indata))
print("the file content: %r" % indata)print("Does the output file exist? %r" % exists(to_file)) # 判断文件存在print("Ready, hit RETURN to continue, CTRL-C to abort.")
input()outputfile = open(to_file, 'w') # 以写的方式打开文件
outputfile.write(indata) # 写入数据print("Alright, all done.")outputfile.close() # 关闭打开的文件
inputfile.close()