python 关于文件的一些操作
2014-01-22 14:39
477 查看
#! /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))
# -*- 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))
相关文章推荐
- 轻松python之文件专题-关于目录树的一些操作
- 轻松python之文件专题-关于目录树的一些操作
- 轻松python之文件专题-关于目录树的一些操作
- 轻松python之文件专题-关于zip文件的一些操作
- 轻松python之文件专题-关于zip文件的一些操作
- 轻松python之文件专题-关于zip文件的一些操作
- 关于python文件操作
- 关于python文件操作
- PYTHON对文件及文件夹的一些操作
- 【Linux基础学习之一】关于Linux的一些基本操作(linux登陆、口令文件、多终端登陆)
- 关于python文件操作
- [转]Python中关于文件路径的简单操作
- 关于python文件操作 (转载)
- Python语言关于文件操作
- 关于python文件操作
- 关于python文件操作
- python关于文件的操作
- 转:关于python文件操作大全
- 关于python文件操作
- 想在每个文件头部加一些关于该文件的信息,如作者,创建时间,用途等,大家一般是怎么操作的?