本文主要是介绍《笨办法学python》习题17:更多文件操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
from sys import argv
from os.path import exists #我们 import 了又一个很好用的命令 exists。这个命令将文件名字符串作为参数,如果文件存在的话,它将返回 True,否则将返回 False。script, from_file, to_file = argvprint "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:更多文件操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!