1.文章中涉及的脚本
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)
# we could do these two on one line too, 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.write(indata)
print "Alright, all done."
out_file.close()
in_file.close()
2.运行结果
#shell中输入
python q.py q1.txt q2.txt # q1文本内容复制到q2
#结果:
Copying from q1.txt to q2.txt
The input file is 9 bytes long
Does the putput file exist?True
Ready,hit RETURN to continue,CTRL-C to abort.
RETURN
Alright,all done.
#shell中输入
cat q2.txt
#结果:
11
22
33
3.思考题:脚本改多短
from sys import argv#导入模块
script,from_file,to_file=argv#导入参数
with open(from_file) as file1,open(to_file,'w') as file2:
indata=file1.read()
file2.write(indata)
4. WindowsPowerShell ,cat 的替代品--type命令
PS D:\>type q1.txt #打开文件,展示内容
11
22
33
44
<参考文章链接:【Windows】Windows中是否有代替Linux中cat的命令_'Ablaze 的专栏-CSDN博客>
485

被折叠的 条评论
为什么被折叠?



