#! /usr/bin/python
# -*- coding:utf-8 -*-
'''
Created on 2013-12-11
@author: Java
'''
import os
import MySQLdb
from db.DbUtil import DbUtil
import time
import shutil
import sys
import zipfile
from os.path import join,getsize
import math
class FileOption():
#global fileList 类的全局变量 放置位置
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 renameFile(self,path):
'''
批量修改文件的名称
:param path:
'''
self.path = path
allRenameList = os.listdir(path)
for allRenameItem in allRenameList:
renamePath =os.path.join(path,allRenameItem)
if os.path.isfile(renamePath)==True:
print allRenameItem
if allRenameItem.find('.')<0:
newname = allRenameItem+'index.html'
os.renames(os.path.join(path,allRenameItem), os.path.join(path,newname))
print allRenameItem,'ok'
else:
os.renames(os.path.join(path,allRenameItem), os.path.join(path,'index.html'))
print allRenameItem,'ok'
def renameFile2(self,path,newName):
'''
批量修改文件名称,解决了中文文件名称的问题
:param path:路径
:param newName:新名称
'''
allRenameList = os.listdir(path)
for allRenameItem in allRenameList:
renamePath =os.path.join(path,allRenameItem)
if sys.getfilesystemencoding()=='mbcs':
renamePath=renamePath.decode('mbcs')
print renamePath
elif sys.getfilesystemencoding() & 0x800:
renamePath=renamePath.decode('utf-8')
# print renamePath
if os.path.isfile(renamePath)==True:
if sys.getfilesystemencoding()=='mbcs':
allRenameItem=allRenameItem.decode('mbcs')
elif sys.getfilesystemencoding() & 0x800:
allRenameItem=allRenameItem.decode('utf-8')
if allRenameItem.find('.')<0:
newname = allRenameItem+newName
os.renames(os.path.join(path,allRenameItem), os.path.join(path,newname))
print allRenameItem,'ok'
else:
os.renames(os.path.join(path,allRenameItem), os.path.join(path,newName))
print allRenameItem,'ok'
else:
option = FileOption()
option.renameFile2(renamePath,newName)
def IteratrFolder(self,testpath):
'''
遍历文件夹 并且解决了中文文件名的问题
查看系统文件名编码 sys.getfilesystemencoding()
:param path:
'''
listsubDir=[]
list = os.listdir(testpath)
for filename in list:
if sys.getfilesystemencoding()=='mbcs':
filename=filename.decode('mbcs')
paths = os.path.join(testpath,filename)
if os.path.isdir(paths):
listSub = os.listdir(paths)