源程序:
from sys import argv
from os.path import exists
##从os.path模块中导入exists命令
script from_file, to_file = argv
print "Coping from %s to %s" %(from_file, to_file)
in_file=open(from_file)
indata=in_file.read()
##将from_file文件打开并读入其中数据 储存到indata变量中
print"The input file is %d bytes long" %len(indata) ##indata长度
print "Does the output file exist? %r" %exists(to_file) ##此时没有将数据读入to_file 所以不存在
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input() ##需要用户键入什么东西???
out_file=open(to_file, 'w') ##将to_file数据传入out_file
out_file.write(indata) ##将indata中数据写入out_file中
print "Alright, all done."
out_file.close()
in_file.close()
命令行输入:
echo "This is a test file." > test.txt ##利用echo建立一个test.txt文件 文件内容为 This is a test file.
$ cat test.txt
## cat
This is a test file.
$
$ python ex17.py test.txt new_file.txt
Copying from test.txt to new_file.txt
The input file is 21 bytes long
Does the output file exist? False
Ready, hit RETURN to continue, CTRL-C to abort.
This is a test file.
$
$ python ex17.py test.txt new_file.txt
Copying from test.txt to new_file.txt
The input file is 21 bytes long
Does the output file exist? False
Ready, hit RETURN to continue, CTRL-C to abort.
Alright, all done.
echo命令是把echo后面的内容作为字符串显示出来
cat 命令的用途是打印文件到屏幕上
想看一个文件的内容是什么,可以用cat,比如 cat/etc/reslov.conf,就显示出文件reslov.conf里具体的内容。
比如 echo hello,就显示hello;
比如 echo /etc/reslov.conf ,会显示/etc/reslov.conf
问题1:from_file & to_file 两个变量是新定义的