ex17.py

#-*- coding=utf-8 -*-
#ex17.py

from sys import argv
from os.path import exists

script,from_file,to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

input_txt = open(from_file)         #默认是只读打开

indata = input_txt.read() 
print "The input file is %d bytes long" %( input_txt.tell()-1) #测试tell()
print "The input file is %d bytes long" % len(indata) 
 
print "Does the output file exist? %r" % exists(to_file) #exists(文件名)  检测文件是否存在
print "Ready, hit RETURN to continue, CTRL-C to abort." 
raw_input() 

output_txt = open(to_file, 'w') 
output_txt.write(indata) 

print "Alright, all done." 
 
output_txt.close() 
input_txt.close() 

运行结果:


运行结果

本程序实现了文本的复制,使用exists()进行了文件的判断,文件字节数的读取

你可能感兴趣的:(ex17.py)