from sys import argv
from os.path import exists #我们 import 了又一个很好用的命令 exists。这个命令将文件名字符串作为参数,如果文件存在的话,它将返回 True,否则将返回 False。
script, from_file, to_file = argv
print "Copying from %s to %s" % (from_file, to_file)
# we could do these two on one line too, how?
input = open(from_file)
indata = input.read()
print "The input file is %d bytes long" % len(indata)
print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()
output = open(to_file, 'w') #将from_file的内容复制到to_file
output.write(indata)
print "Alright, all done."
output.close()
input.close()
《笨办法学python》习题17:更多文件操作
最新推荐文章于 2021-05-04 11:41:47 发布