在写python的脚步的时候,写个总结。
Os 模块
在Python中,Python递归文件操作主要来自os模块,主要方法如下:
1. os.listdir(dirname):列出dirname下的目录和文件
2. os.getcwd():获得当前工作目录
3. os.curdir:返回当前目录('.')
4. os.chdir(dirname):改变工作目录到dirname
5. os.path.isdir(name):判断name是不是一个目录,
name不是目录就返回false
6. os.path.isfile(name):判断name是不是一个文件,
不存在name也返回false
7. os.path.exists(name):判断是否存在文件或目录name
8. os.path.getsize(name):获得文件大小,如果name是目录返回0L
9. os.path.abspath(name):获得绝对路径
10. os.path.normpath(path):规范path字符串形式
11. os.path.split(name):分割文件名与目录
一些其他比较有用的操作
1. os.path.splitext():分离文件名与扩展名
2. os.path.join(path,name):连接目录与文件名或目录
3. os.path.basename(path):返回文件名
4. os.path.dirname(path):返回文件路径
5. os.remove(dir) #dir为要删除的文件夹或者文件路径
6. os.rmdir(path) #path要删除的目录的路径
7. os.path.gettime(file):得到file的最后一次修改的时间。
需要说明的是, 使用os.rmdir删除的目录必须为空目录,否则函数出错。 以上的文章就是对Python递归文件操作函数概述。
os模块提供了对操作系统功能的访问接口,并且是平台无关的。os.environ变量存放的是映射对象,其项为环境变量名及其值。程序的工作目录可由os.getcwd()提供,并可以使用os.chdir()修改。该模块还提供了一些函数,可用于实现底层基于文件描述符的文件处理。os.access()函数可用于确定某个文件是否存在,或者文件是否可读/可写;os.listdir()函数可以返回给定目录中的条目列表(比如,文件与目录,但排除.条目与..条目);os.stat()函数返回关于文件与目录的各种信息项,比如模式、访问时间与大小等。
目录可以使用os.mkdir()创建,或者,如果需要创建中间目录就使用os.make- dirs()。空目录可以使用os.rmdir()移除,只包含空目录的目录树可以使用os.removedirs()移除。文件与目录都可以使用os.remove()移除,也可以使用os.rename()重命名。
os.walk()函数可以在整个目录树上进行迭代,依次取回每个文件与目录的名称。
os模块也提供了很多底层的平台特定的函数,比如,操纵文件描述符的函数,以及fork(仅适用于UNIX系统)、spawn与exec等。
os模块提供的函数主要是与操作系统进行交互,尤其对文件系统,os.path模块则提供了字符串(路径)操纵函数与便于操纵文件系统的函数的混合。os.path.abspath()函数可以返回其参数的绝对路径,并移除冗余的路径分隔符与..元素;os.path.split()函数返回一个二元组,其中第一项包含路径,第二项则是文件名(如果某个路径中没有文件名,此项就为空),这两项也可以直接使用os.path.basename()与os.path.dirname()获取。文件名也可以分为两个部分,即名称与扩展名,这是使用os.path.splitext()实现的。os.path.join()函数可以接受任意数量的路径字符串,并使用平台特定的分隔符返回单一的路径。
如果需要获取某个文件或目录的多项信息,就可以使用os.stat(),但是如果只需要某种单一的信息,就可以使用相关的os.path函数,比如,os.path.exists()、os.path.getsize()、os.path.isfile()或os.path.isdir()。
Shutil模块
Shutil模块提供了用于文件与目录处理的高层函数,包括用于复制文件与整个目录的shutil.copy()函数与shutil.copytree()函数,用于移动目录树的shutil.move()函数,以及用于删除整个目录树(包括非空的)的shutil.rmtree()函数。
临时文件与目录应该使用tempfile模块创建,该模块提供了必要的函数,比如 tempfile.mkstemp(),并以尽可能安全的方式创建临时对象。
filecmp模块可用于对文件进行比较(使用filecmp.cmp()函数),也可以用于对整个目录进行比较(使用filecmp.cmpfiles()函数)。
注意的是的
shutil.copytree(“source_dir”,”destination_dir”)
destination_dir目录必须不存在,否则会报错.
文中的“\\”表现的有点问题,我用python自带的编辑器,是没有问题的!
def file_move(source_dir,destination_dir,Listname):
'''
description: coye file and folder from source_dir to destination_dir
param source_dir: the source directory
param destination_dir: the destination directory
param Listname: the file in which the file or folder
'''
file=open(Listname)
#check the destination_dir exist
if(os.path.isdir(destination_dir)):
shutil.rmtree(destination_dir)
for line in file:
temp=line.strip()
print temp
if(temp==""):
continue
#check the variable is a file or not
if (os.path.isfile(source_dir+"\\"+temp)):
destination_file_path=os.path.dirname(destination_dir+"\\"+temp)
#check the path of file exist
if(os.path.exists(destination_file_path)):
shutil.copy(source_dir+"\\"+temp,destination_dir+"\\"+temp)
else:
os.makedirs(destination_file_path)
shutil.copy(source_dir+"\\"+temp,destination_dir+"\\"+temp)
#check the variable is a dir or not
elif(os.path.isdir(source_dir+"\\"+temp)):
#copy the tree under this dir
shutil.copytree(source_dir+"\\"+temp, destination_dir+"\\"+temp, False)
else:
#search the path before the "(except" in this line
p1=re.compile(r'.*?(?=\(except)')
m1=p1.search(temp)
#search the fileList in the "(except .....)" in this line
p2=re.compile(r'(?<=\(except).*?(?=\))',re.M|re.S)
m2=p2.search(temp)
if m1:
path=m1.group()
#check the variable:path is a dir or not
if(os.path.isdir(source_dir+"\\"+path.strip())):
shutil.copytree(source_dir+"\\"+path.strip(), destination_dir+"\\"+path.strip(), False)
if m2:
fileListTemp=m2.group()
#get the files in the () and put them into fileList
fileList=fileListTemp.split(',')
#delete the file in the fileList
file_delete(destination_dir+"\\"+path.strip(),fileList)
else :
print "no file to delete"
else:
print "no this"+destination_dir+"\\"+path.strip()+"path"
continue
else:
print "please check "+source_dir+"\\"+temp
file.close
解释下我的代码:
List里面是一些file或者文件夹的路径。
oal/common
oal/include
oal/wince
oal/Makefile.gnu
ial/gallium/emgd/src/gallium/Makefile.gnu
ial/gallium/imports/src/gallium/auxiliary/util (except u_cpu_detect.c,u_cpu_detect.h,u_mm.c,u_mm.h,u_snprintf.c )
之类的。
1if(os.path.isdir(destination_dir)): shutil.rmtree(destination_dir)
因为
shutil.copytree(“source_dir”,”destination_dir”)
destination_dir目录必须不存在,否则会报错.
2temp=line.strip()
读取文件的时候总是会有\n\r或者\n什么的,这样你再去做判断是否是文件(目录)的时候会失败
strip()去掉两边的空格一类的东西。
3if(temp==""): continue
因为是读取文件的一行,曾经出现这样的问题,最后多了一个回车,
去掉空格以后,然后继续做比较,结果是目录。就拷贝目录,但是这个目录已经存在了。
4
if (os.path.isfile(source_dir+"\\"+temp)): destination_file_path=os.path.dirname(destination_dir+"\\"+temp)
#check the path of file exist if(os.path.exists(destination_file_path)): shutil.copy(source_dir+"\\"+temp,destination_dir+"\\"+temp) else: os.makedirs(destination_file_path) shutil.copy(source_dir+"\\"+temp,destination_dir+"\\"+temp)
拷贝的时候要判断文件的路径是否存在,shuilt.copy不会自己创建文件目录
5
p1=re.compile(r'.*?(?=\(except)')
正则表达式:找到(except前面的
p2=re.compile(r'(?<=\(except).*?(?=\))',re.M|re.S)
找到(except开头和)结尾的中间匹配的部分。