python文件操作

#! /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)
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值