電腦

當前位置 /首頁/遊戲數碼/電腦/列表

python 文件和文件夾的新建和刪除操作

文件夾的新建和刪除,文件的複製,查找,自己二次加工了一個類

操作方法

(01)# -*- coding: utf8 -*-'''base function 基礎功能'''import sys,os,timeimport shutilreload(sys)efaultencoding('utf-8')class Chk_file(object):"""docstring for Chk_file"""def __init__(self):super(Chk_file, self).__init__()def endWith(self,s,*endstring):array = map(with,endstring)if True in array:return Trueelse:return False

python 文件和文件夾的新建和刪除操作

(02)def Mkdir(self,file_path):#文件夾自動創建if not r(file_path):print (file_path+u' is not exists')print (u'Ready to create')dirs(file_path)print (u'Create Success')else:print(file_path+u' is exists')

python 文件和文件夾的新建和刪除操作 第2張

(03)def DELdir(self,file_path):#刪除文件夾if r(file_path):print (u'準備刪除'+file_path)ee(file_path)print (u'刪除'+file_path+u'成功!')else:print(u'文件夾'+file_path+u'已刪除')

python 文件和文件夾的新建和刪除操作 第3張

(04)def Copyfile(self,file,newfile=None):複製文件並添加當前時間重命名Time=time('%Y%m%d%H%M%S',ltime(()))if ts(file):print(u'start copyfile')if newfile==None:src_name,file_ext = text(file)else:src_name,file_ext = text(newfile)newfile_path=src_name+Time+file_file(file,newfile_path)print(u'Copy Success')else:print(u'%s is not exists'%file)

python 文件和文件夾的新建和刪除操作 第4張

(05)def Chk_dir(self,file_path,end_name):#批量查找文件夾下符合擴展名要求的文件並打印不符合要求的文件#文件夾不存在就自動創建Isfile=Falselegal_docs=[]illegal_files=[]file_names = dir(file_path)if file_names is not None and file_names != []:for file_name in file_names:file=(file_path,file_name)if ith(file,end_name):legal_nd(file)Isfile=Trueelse:illegal_nd(file_name)print (file_path+u'存在非法:'+file_name)if illegal_files !=[]:print (file_path+u'非法文件列表:'+','(illegal_files))else:print(u'No illegal files(沒有非法文件)')if Isfile:print (file_path+u'合法文件列表:'+','(legal_docs))return legal_docselse:print (u'NO file:沒有合法文件')print (u'所有文件都是非法格式,請再次確認文件,合法格式有:'+str(end_name))return Falseelse:print (u'NO file(文件夾沒有文件)')return False

python 文件和文件夾的新建和刪除操作 第5張

特別提示

自己寫的一個類