python文件读写demo

来源:互联网 发布:python 加载c 静态库 编辑:程序博客网 时间:2024/05/16 04:57

python按行读取文件

#! /usr/bin/pythonsrcFile = "test.src" #root of src filedstFile = "test.dst" #root of dst filesf = open(srcFile , 'r')df = open(dstFile , 'w')for line in sf:#here you can do something with linedf.write(line)sf.close()df.close()

python在文件头部插入内容

#! /usr/bin/pythonFile = "test.src" #root of filefile = open(File , 'r')HistoryContent = file.read()file.close()file = open(srcFile , 'w')file.write("new content\n")#here can be replaced by new contentfile.write(HistoryContent)file.close()


原创粉丝点击