python遍历文件,python遍历,下面的python代码片
python遍历文件,python遍历,下面的python代码片
下面的python代码片段通过os.walk方法遍历目录,并删除大于50M的文件。
# filename itertaorfilefolder import os import os.pathfilePath = raw_input('Enter filepath : ')#遍历文件夹 #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 for parent ,dirnames , filenames in os.walk(filePath): #输出文件夹信息 for dirname in dirnames: print 'parent is :'+parent print 'dirname is '+ dirname #输出文件信息 for filename in filenames : print 'parent is :'+parent print 'filename is :' + filename #输出文件路径信息 currentPath = os.path.join(parent,filename) print 'the fulll name of the file is :'+ currentPath filesize = os.path.getsize(currentPath)/1024/1024 print 'the file size is : %.3f MB' %(filesize) #删除大于50m的文件 if filesize > 50: delete = raw_input(' are you sure to delete ?') if delete == 'yes': os.remove(currentPath)
相关内容
- jython编译java代码并执行,jython编译java,下面的代码片段
- python的lambda表达式实现递归,pythonlambda递归,[Python]代码
- Python sql注入检测工具代码,pythonsql注入代码,Python sql注
- python解决数独问题,python解决数问题,[Python]代码#
- wxpython下输出错误到控制台console,wxpythonconsole,import s
- 利用pyHook截取人人登陆密码,pyhook截取登陆密码,客户端
- python多线程ping和arpping扫描工具,pythonarpping,多线程pi
- python向mysql中插入数据时出现_mysql_exceptions.OperationalEr
- BeautifulSoup中文乱码问题解决,beautifulsoup乱码,Beautiful
- Python采集百度地图数据,python采集地图,百度利用其强大
评论关闭