1.os模块的常用目录处理函数
mkdir(path,[mode=0777]) 创建一个path指定的目录,mode为目录的权限设置,默认为0777(读取、写入、执行)
makedirs(name,mode=511) 创建多级目录,如'd:/path1/path2/'则在d盘中新建一个path1然后再在path1目录中新建一个path2
rmdir(path) 删除path指定的目录
removedirs(path) 删除path指定的多级目录
listdir(path) 返回path指定目录下的所有文件和目录(不包括子目录的文件)
getcwd() 返回当前的工作目录
chdir(path) 将当前的工作目录改变为path指定的目录
walk(top,topdown=True,onerror=None) 遍历目录树(返回top指定的目录中的所有文件和子目录与子目录文件)
该函数返回一个三个元素的元组,这三个元素分别是每次变量的路径名、目录列表、文件列表
topdown参数有两种True和False默认是True
True表示先返回根目录下的文件然后再返回子目录与子目录的文件
False则表示先返回子目录和子目录中的文件再返回根目录中的文件
onerror参数可以提供一个在遍历目录产生错误时处理该错误的处理函数,默认是None也就是忽略错误
2.os.path模块常用目录处理函数:
abspath(path) 返回path所在的绝对路径
dirname(path) 返回path的目录名称
exists(path) 判断path是否存在,存在返回True,反之False
getatime(filename) 返回filename的最后访问时间
getctime(filename) 返回filename的创建时间
getmtime(filename) 返回filename的最后修改时间
getsize(filename) 返回filename的大小
isabs(path) 判断path是否为绝对路径,是返回True(就算path不存在)反之False
isdir(path) 判断path是否为目录,是返回True反之False(如果path不存在也返回False哦)
isfile(path) 判断path是否问文件,是返回True反之False(如果path不存在也返回False哦)
split(path) 对path进行路径分割,返回一个以路径、非路径这个两个元素组成的元组
splitext(path) 从path中分割扩展名名称返回两个元素的元组(非扩展名名称、扩展名名称)
splitdrive(path) 从path中分割驱动器名称返回两个元素的元组(驱动器名称、非驱动器名称)
walk(top,func,arg) 遍历目录,与os.walk()功能相同
3.目录文件的遍历
在Python中有三种实现目录遍历的方法:①-递归函数、②-os.path.walk()、③-os.walk()
①-自定义递归函数进行遍历
>>> import os
>>> def VisitDir(path): #定义函数
dirlist = os.listdir(path) #使用os模块的listdir()返回目录和文件列表(不包括子目录文件)
for listname in dirlist: #遍历刚才获取得到的文件和目录列表
dirname = os.path.join(path,listname): #把当前遍历到的文件或目录名称与根目录path连接起来(也就是完整路径)
if os.path.isdir(dirname): #如果当前路径是目录
VisitDir(dirname) #使用函数递归再次处理当前路径(也就是使用本函数再次执行以上操作)
else: #如果当前路径不是目录,也就是说当前路径是一个文件
print dirname #输出完整路径的文件名称
>>>if __name__ == '__main__':
path = 'd:\code' #注意这里的路径最后最好不要有一个'\'否则Python视为新建逻辑行操作
VisitDir(path) #调用函数
d:\code\son1\1.txt #遍历成功,
d:\code\son2\2.txt
d:\code\son3\3.txt
d:\code\123.txt
后记:这个方法遍历目录的时候只能返回有文件的目录,可以在调用递归前输出一下目录就可以返回所有目录了
②-os.path.walk(top,func,art)
后记:暂缺待续
③-os.walk(top,topdown=True,onerror=None)
top参数表示需要遍历的目录树路径
topdown参数有两个选择True和False(如不提供默认为True)
当为True时则表示先遍历完了路径根目录的文件后再遍历子目录和子目录文件
当为False的时候则表示先遍历路径的子目录和子目录文件后在遍历路径根目录的文件
onerror参数的默认值是None,表示忽略遍历过程中产生的错误,如果提供参数则需提供一个处理错误的自定义函数
该函数返回一个共有三个元素的元组,三个元素分别是每次遍历的路径名称、目录列表、文件列表(注意后两个元素是列表)
#!bin/usr/python
#-*-coding:UTF-8-*-
#filename:filelist.py
import os
def VisitDir(path):
for root,sonroot,filelist in os.walk(path):
for filename in filelist:
print os.path.join(root,filename)
if __name__ == '__main__':
VisitDir(raw_input('需要遍历的目录:'))
>>>
需要遍历的目录:d:\code
d:\code\123.txt
d:\code\filelist.py
d:\code\filelist2.py
d:\code\son1\1.txt
d:\code\son2\2.txt
d:\code\son3\3.txt
>>>
文件属性程序:
#!/bin/usr/python
#-*-coding:UTF-8-*-
#filename:Fileproperties.py
import os
import time
def Show_File_Properties(path):
'''
Function_Name:Show_File_Properties(path)
显示文件的属性-包括路径、大小、创建时间、最后修改时间、最后访问时间
'''
for root,dirlist,filelist in os.walk(path):
print '路径:' + root
for filename in filelist:
state = os.stat(os.path.join(root,filename))
info = '文件名:' + filename + '\n'
info = info + '大小:' + ('%d' %state[-4]) + ' Byte\n'
t = time.strftime('%Y-%m-%d %X',time.localtime(state[-1]))
info = info + '创建时间:' + t + '\n'
t = time.strftime('%Y-%m-%d %X',time.localtime(state[-2]))
info = info + '修改时间:' + t + '\n'
t = time.strftime('%Y-%m-%d %X',time.localtime(state[-3]))
info = info + '访问时间:' + t + '\n'
print info
if __name__ == '__main__':
path = raw_input('路径:')
Show_File_Properties(path)