python文件操作学习笔记

发布时间:2020-07-08 19:28:47 作者:知止内明
来源:网络 阅读:356


#文件操作:

读:

f = open("/Users/zhouhaijun/python/01.py","r")

x = f.read()

print x


写:

f = open("/Users/zhouhaijun/python/file_01.py","wb")

f.write("ok")

f.close()


读:

f = open("/Users/zhouhaijun/python/file_01","r")

text = f.read() #text = f.read(100)

print text

读取一行:

print f.readline()  #按行读取


接下来的函数是拷贝文件,一次读写五十个字符。第一个参数是源文 件名,第二个参数是新文件名 


def copyFile(oldfile, newfile):

f1 = open(oldfile,"r")

f2 = open(newfile,"w")

while 1:

text = f1.read(50)

if text == "":

break

f2.write(text)

f1.close()

f2.close()

return



接下来的例子是行处理程序,函数filterFile拷贝一个文件,同时将旧

文件中不是以“#”开头的行写入新文件中:

def fileterFile(old, new):

sfile = open(old, "r")

dfile = open(new, "w")

while 1:

text = sfile.readline()

if text =="":

break

elif:

text[0] = "#":

continue

else:

dfile.write(text)

sfile.close()

dfile.close()


推荐阅读:
  1. Python 3 学习笔记:序列
  2. python学习笔记

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

文件 操作 python

上一篇:Yii 2 —— 基于Mustache的页面多语言解决方案

下一篇:Java微服务新生代之Nacos

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》