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张

特别提示

自己写的一个类