- 论坛徽章:
- 0
|
3)文件操作
打开文件进行读写
import os
input=open(filename ,'r') #读文件
output=open(filename ,'w') #写文件
output=open(filename,'a') #追加写文件
遍历文件夹操作
import os
for root, dirs, files in os.walk(path, topdown=False):
#hanlde file
for name in files:
if name[:-3] = 'exe':
print name
#删除文件
top='mydata/'
for root,dir,files in os.walk(top,topdown=False):
for name in files:
os.remove(os.path.join(root,name))
os.rmdir('mydata')
os.mkdir('mydata')
列出文件
import os
os.listdir("c:\\music\\_singles\\")
['a_time_long_forgotten_con.mp3', 'hellraiser.mp3', 'kairo.mp3',
'long_way_home1.mp3', 'sidewinder.mp3', 'spinning.mp3']
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/29089/showart_272836.html |
|