exercise17 更多文件

源程序:
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.
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 两个变量是新定义的






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值