python r只读 w可写 a追加
1 可写
ff = open('aa.txt','w')
ff.write('hello,world')
ff.close()
2 追加
ff = open('aa.txt','w')
ff.write('hello,world')
ff.close()
ff = open('aa.txt','a')
for i in range(0,10):
ff.write(str(i))
ff.close()
3 读取
ff = open('aa.txt','w')
ff.write('hello,world\nooxx')
ff.close()
ff = open('aa.txt','r')
for i in ff.readlines():
print i
ff.close()
4 删除文件
import os
file = 'aa.txt'
os.remove(file)
1 可写
ff = open('aa.txt','w')
ff.write('hello,world')
ff.close()
2 追加
ff = open('aa.txt','w')
ff.write('hello,world')
ff.close()
ff = open('aa.txt','a')
for i in range(0,10):
ff.write(str(i))
ff.close()
3 读取
ff = open('aa.txt','w')
ff.write('hello,world\nooxx')
ff.close()
ff = open('aa.txt','r')
for i in ff.readlines():
print i
ff.close()
4 删除文件
import os
file = 'aa.txt'
os.remove(file)
本文介绍了使用Python进行文件的基本操作,包括以不同模式打开文件(只读、可写、追加),如何向文件中写入内容及按行读取文件内容,并展示了如何利用Python内置的os模块来删除指定文件。
4701

被折叠的 条评论
为什么被折叠?



