Python文件的一些操作代码
发布时间:2020-12-17 17:26:07 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #! /usr/bin/python# -*- coding:utf-8 -*-'''Created on 2013-12-11@author: Java'''import osimport MySQLdbfrom db.DbUtil import DbUtilclass Fil
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 #! /usr/bin/python # -*- coding:utf-8 -*- ''' Created on 2013-12-11 @author: Java ''' import os import MySQLdb from db.DbUtil import DbUtil class FileOption(): def __init__(self): pass def CreateFloderByList(self,pathList): ''' 创建文件夹 :param pathList:文件夹集合 ''' self.pathList = pathList for path in pathList: if not os.path.isdir(path): os.makedirs(path) def CreateFloder(self,path): self.path = path if not os.path.isdir(path): os.makedirs(path) def readFileName(self,path): fileNames = os.listdir(path) return fileNames def copyFiles(self,sourceDir,targetDir): for file in os.listdir(sourceDir): sourceFile = os.path.join(sourceDir,file) targetFile = os.path.join(targetDir,file) if os.path.isfile(sourceFile): if not os.path.exists(targetDir): os.makedirs(targetDir) if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))): open(targetFile,"wb").write(open(sourceFile,"rb").read()) if os.path.isdir(sourceFile): First_Directory = False create = FileOption() create.copyFiles(sourceFile,targetFile) def moveFiles(self,dir): '''根据文件名创建文件夹,并将其放入对应的文件夹内''' for i in os.listdir(dir): name = ''.join(i.split('.html')[0:-1]) print name os.mkdir(os.path.join(dir,name)) os.rename(os.path.join(dir,i),os.path.join(dir,name,i)) 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |