python 檔案和資料夾的新建和刪除操作

來源:酷知科普網 1.53W

檔案夾的新建和刪除,檔案的複製,查詢,自己二次加工了一個類

操作方法

(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張

特別提示

自己寫的一個類

熱門標籤