Python文件的一些操作代碼

em7 9年前發布 | 5K 次閱讀 Python

#! /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)) </pre>

 本文由用戶 em7 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!