首页 > 其他 > 详细

更多的读写

时间:2016-06-08 20:25:33      阅读:294      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!