笨办法学Python ex17

2016-12-11  本文已影响0人  Joemini

更多文件操作


# -- coding: utf-8 --
# 下面的代码是用来创建一个测试文件
from sys import argv

script, a = argv

b = open(a,'w')

b.truncate()

b.write('This is a test file.')

b.close()

# 下面是复制命令
from sys import argv
from os.path 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, how?
in_file = open(from_file)
indata = in_file.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()

out_file = open(to_file, 'w') # 对象文件复制于out_file
out_file.write(indata) # 将需复制的内容写入对象文件

print "Alright, all done."

out_file.close()
in_file.close()

遇到的问题

上一篇下一篇

猜你喜欢

热点阅读