码迷,mamicode.com
首页 > 其他好文 > 详细

更多的读写

时间:2016-06-08 20:25:48      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

说是从一个文件复制到另一个文件,

其实还是读写啦,参考:习题—17

# coding: utf-8

from sys import argv
from os.path import exists

script, from_file, to_file  = argv                                 # 将argv解包给script, from_file, to_file三个参数

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

from_txt = open(from_file)
content = from_txt.read()

print "...The file has %s bytes." % len(content)           # len()用于查询字符串长度
print ">>>Does the output file exist? %r" % exists(to_file)

print "...If you want to continue, hit Enter, or not, hit Ctrl+C."
raw_input("> ")

to_txt = open(to_file, ‘w‘)                                        # 以‘w‘写的模式打开,原有内容会被清空
to_txt.write(content)

print "Copy has done...and eixt!"

from_txt.close()
to_txt.close()                                                           # 有开就有关,记得关闭哦

更多的读写

标签:

原文地址:http://www.cnblogs.com/Ruby517/p/5571212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!