《笨办法学python》习题17:更多文件操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/skyejy/article/details/79647234
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()

猜你喜欢

转载自blog.csdn.net/skyejy/article/details/79647234