python基础学习总结——文件操作
写文件
text="hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
f = open('1.txt','w')
f.write(text)
f.close()
open的第二个参数表示打开文件的模式,详细解释如下:
w 以写方式打开,
a 以追加模式打开
r+ 以读写模式打开
w+ 以读写模式打开
a+ 以读写模式打开
rb 以二进制读模式打开
wb 以二进制写模式打开
ab 以二进制追加模式打开
rb+ 以二进制读写模式打开
wb+ 以二进制读写模式打开
ab+ 以二进制读写模式打开
读文件
f=open('a.txt')
while True:
line = f.readline()
if len(line) ==0:
break
print(line)
f.close()
代码比较好懂,所以不解释了
创建文件夹
import os
#判断当前项目目录下有没有name文件夹
if not os.path.exists('name'):
#创建name文件夹
os.makedirs('name')
还没有评论,来说两句吧...