更多的读写
Posted 千秋邈矣独留我
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了更多的读写相关的知识,希望对你有一定的参考价值。
说是从一个文件复制到另一个文件,
其实还是读写啦,参考:习题—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() # 有开就有关,记得关闭哦
以上是关于更多的读写的主要内容,如果未能解决你的问题,请参考以下文章