您的位置:首页 > 编程语言 > Python开发

python文件读写demo

2013-11-24 16:06 225 查看
python按行读取文件

#! /usr/bin/python

srcFile = "test.src" #root of src file
dstFile = "test.dst" #root of dst file
sf = open(srcFile , 'r')
df = open(dstFile , 'w')
for line in sf:
#here you can do something with line
df.write(line)
sf.close()
df.close()


python在文件头部插入内容

#! /usr/bin/python

File = "test.src" #root of file
file = open(File , 'r')
HistoryContent = file.read()
file.close()
file = open(srcFile , 'w')
file.write("new content\n")#here can be replaced by new content
file.write(HistoryContent)
file.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: