python学习6——拷贝文件

一、拷贝文件。

首先建立一个空的txt文档,命名为11.txt。

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= open(from_file)
indata = input_f.read()
print("The input files 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.")
input()
output = open(to_file,'w')
output.write(indata)
print("Alright, all done.")
output.close()
input.close()

输出结果:

E:\abc>python 11.py 10.txt 11.txt
Copying from 10.txt to 11.txt.
The input files is 95 bytes long
Does the output file exist? True
Ready, hit RETURN to continue, CTRL_C to abort.
Traceback (most recent call last):
File "11.py", line 10, in <module>
input()
TypeError: '_io.TextIOWrapper' object is not callable

出现错误:_io.TextIOWrapper' object is not callable

原因:我一直按照《笨办法学python》做练习,但是该书中的raw_input不适用于python3,所以我将raw_input改为input,但是这个脚本第五行已经出现了input,第11行的input就无法执行,所以要将其修改,此处改为input_f

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_f = open(from_file)
indata = input_f.read()
print("The input files 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.")
input()
output = open(to_file,'w')
output.write(indata)
print("Alright, all done.")
output.close()
input_f.close()

输出结果:

同时查看11.txt文件内容与10.txt文件内容一致,即拷贝成功。

 

posted on 2018-08-23 16:53  shannon_V  阅读(843)  评论(0)    收藏  举报

导航