python文件操作
来源:互联网 发布:天心软件集团 编辑:程序博客网 时间:2024/06/17 04:21
1、文件遍历、查找,移动
2、删除指定后缀文件
3、判断文件是否存在
4、文件夹创建
def produce_time_desc():
#取到当前时间localtime = time.localtime(time.time())str_time = time.strftime('-%Y%m%d-%H%M%S',localtime)return str_time
遍历某文件夹,获取全部文件名,形成数组。文件夹滤掉。
def find_file_ori_imei():
print ‘start to find file from ori dir’
file_array = os.listdir(ori_imei_dir)
imei_ary = []
for file_name in file_array:
if not os.path.isdir(ori_imei_dir+'/'+file_name): if len(file_name) > 10: imei_ary.append(file_name) pass passreturn imei_ary
查找指定后缀的文件名.x
def delete_file_in_license_dir_with_x(name):
file_will_delete_dir=license_imei_dir+'/'+namefor root, dirs, files in os.walk(file_will_delete_dir, True): for name in files: print name pathname = os.path.splitext(os.path.join(root, name)) print pathname[1] if (not ".xxx" != pathname[1]): os.remove(os.path.join(root, name)) print 'license active file has deleted:'+os.path.join(root,name)pass
def delete_active_file():
print ‘begin to dele_active_file’
imei_ary = find_file_ori_imei()for name in imei_ary: print('will deal with file :'+name) delete_file_in_license_dir_with_x(name) shutil.move(ori_imei_dir+'/'+name, ori_imei_dir+'/'+'backup/'+name+produce_time_desc()) print ('move finish:'+name)pass
主函数
if name==’main‘:
backup_dir=ori_imei_dir+'/backup'if not os.path.exists(backup_dir): os.mkdir(backup_dir) print 'create backup dir'delete_active_file()print 'main function finish'# time.sleep(10)pass
0 0
- Python 文件操作常用操作
- [python]python操作xml文件
- Python文件操作
- python文件目录操作
- Python文件操作
- Python文件操作
- python 文件操作
- python 文件操作
- Python 文件操作
- python 文件操作
- Python文件操作
- python 文件读写操作
- Python文件读写操作
- python文件目录操作
- Python的文件操作
- python常见文件操作
- python - 文件操作
- python文件操作
- 高精度运算
- 手机网络
- Windows Store App异步返回刷新页面
- handler机制原理之精简版
- Windows7内置搜索,同时搜索文件名以及文件里面的内容
- python文件操作
- [Android]OkHttp的简单封装-辅助框架
- Java高级特性之泛型
- CMake安装配置OpenCV3.1
- [生活] 2015年终总结,2016开篇计划
- Android全屏或者取消标题栏 以及可能出现问题
- EL表达式总结
- Backbone View 之间通信的三种方式
- 2015年末总结