python入门基础:文件的读写
发布日期:2021-05-09 08:19:38 浏览次数:16 分类:博客文章

本文共 1650 字,大约阅读时间需要 5 分钟。

������������������������������������������������������������������������������������������������

���������������������������������������������

1���open()������

f = open('workfile', 'w')

������ open()������������������������������������������������������������open(filename, mode)���

  • ������filename ������������������������
#���������������������������   ���������������������������file_abs ="G:\\python\\Notepad python\\07���outputAndInput\\f.txt"
  • ���mode���������������������������������������������������������

1.1���read()������

f = open(file_abs,"r")print(f.read())f.close()

������f������������������������

1.3���readline()������

f.readline() #��������������������������������������������������������������������������������� \n print(f.readline())f.close()

1.4���������������������������������������������������������

for line in f:    print(line,end='')

2������������������   write()

#���������������������������,������������������������\n���������������print(f.write("This is a test in Notepad\n"))

2.1������������������������������

#���������������������������file_abs ="G:\\python\\Notepad python\\07���outputAndInput\\write.txt"    #������������������������with open(file_abs,"w") as f:    f.write("HelloWorld")with open(file_abs, "r") as f:    print(f.read())        #������f������with open(file_abs,"w") as f:    f.write("HelloNewWorld")    #���������������������with open(file_abs,"a") as f:    f.write("fuck the world")

 

 

 

3������������with���������

#��������� with ������������������������������������������#������������������������������������������������������������������������������    with open(file_abs,"r+") as f :    print(f.read())

 

 

 

 

上一篇:windows下如何安装pip以及如何查看pip是否已经安装成功?
下一篇:Python入门基础:代码的编码风格

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月18日 03时02分05秒