功能相当于在path目录下执行dir命令,返回为list类型
举例:
print os.listdir('..')
输出:
[a,b,c,d]
2: os.path.walk(path,visit,arg)
path :是将要遍历的目录
visit :是一个函数指针,函数圆形为:
callback(arg,dir,fileList)
其中arg为为传给walk的arg , dir是path下的一个目录,fileList为dir下的文件和目录组成的list
arg:传给visit用的,对walk没有什么作用
举例:
def callback(arg,directory, files):
print directory,
print files,
print arg
print '--------------------'
os.path.walk('.',callback, '123456')
输出:
. ['path0704.py', 'temp', '\xc2\xb7\xbe\xb6\xcf\xe0\xb9\xd8\xd1\xa7\xcf\xb0.txt'] 123456
--------------------
.\temp ['temp.h', 'temp1'] 123456
--------------------
.\temp\temp1 ['abc.bmp'] 123456
如果想找到某个目录下所有文件,只需要在callback里面,在fileList中找出文件,即可
除此之外,还有一个函数可以用那就是os.walk,看10
3:os.path.split(path)
path 为一个路径,
输出,把path分成两部分,具体看实例:
print os.path.split("abc/de.txt")
('abc', 'de.txt')
os.path.split("abc")
('', 'abc')
print os.path.split("de/abc/de")
('de/abc', 'de')
4: os.path.splitext(filename)
把文件名分成文件名称和扩展名
os.path.splitext(abc/abcd.txt)
('abc/abcd', '.txt')
5: os.path.dirname(path)
把目录名提出来
print os.path.dirname("abc")
#输出为空
print os.path.dirname('abc\def')
abc
6: os.path.basename(filename)
取得主文件名
print os.path.basename('abc')
abc
print os.path.basename('abc.txt')
abc
print os.path.basename('bcd/abc')
abc #这个需要注意不包括目录名称
print os.path.basename('.')
.
7:os.mkdir(path, [mode])
path为目录名: 这里有个要求,只能创建一级目录
比如path为 abc/def 则当前目录下必须存在abc 否则失败
8: os.makedirs(path [,mode])
可以创建多级目录
9:os.remove(path)删除一个文件,一定是一个文件
os.removedirs(path) 删除一个目录下所有东西
os.rmdir(path) 删除一个目录,而且一定要空,否则os.errer
10:os.walk(path)
遍历path,返回一个对象,他的每个部分都是一个三元组
('目录x',[目录x下的目录list],目录x下面的文件)
举例:
a = os.walk('.')
for i in a:
print i
输出:
('.', ['abc', 'temp'], ['path0704.py', '\xc2\xb7\xbe\xb6\xcf\xe0\xb9\xd8\xd1\xa7\xcf\xb0.txt'])
('.\\abc', [], ['\xd0\xc2\xbd\xa8 BMP \xcd\xbc\xcf\xf1.bmp'])
('.\\temp', ['temp1'], ['temp.h'])
('.\\temp\\temp1', [], ['abc.bmp'])
11:shutil.copy(src,dst)
把文件src内容拷贝到文件dst中。,目标区域必须可以写,如果dst存在,则dst被覆盖
上面的函数基本够用
其它文件移动操作还请看:shutil模块:High-level file operations
原文来自:
http://blog.csdn.net/wingSys/archive/2006/07/07/889608.aspx
root_dir = r'X:\XXX\XXX';
def walk(path):
for item in os.listdir(path):
subpath = os.path.join(path, item);
mode = os.stat(subpath)[stat.ST_MODE];
if stat.S_ISDIR(mode):
if item==".svn":
print "Clean %s ..." % subpath;
print "%d deleted!" % purge(subpath);
else:
walk(subpath);
def purge(path):
count = 0;
for item in os.listdir(path):
subpath = os.path.join(path, item);
mode = os.stat(subpath)[stat.ST_MODE];
if stat.S_ISDIR(mode):
count += purge(subpath);
else:
os.chmod(subpath, stat.S_IREAD|stat.S_IWRITE);
os.unlink(subpath);
count += 1;
os.rmdir(path);
count += 1;
return count;
if __name__=='__main__':
walk(root_dir);
借鉴以上代码转自:
http://onlypython.group.javaeye.com/group/blog/53926
root_dir = r'X:\XX\XX';
def purge(path):
count = 0;
for item in os.listdir(path):
subpath = os.path.join(path, item);
mode = os.stat(subpath)[stat.ST_MODE];
if stat.S_ISDIR(mode):
count += purge(subpath);
else:
os.chmod(subpath, stat.S_IREAD|stat.S_IWRITE);
os.unlink(subpath);
count += 1;
os.rmdir(path);
count += 1;
return count;
def callback(arg, directory, files):
if os.path.split(directory)[1]=='.svn':
print directory;
#使用os.removedirs()删不掉
print "Folder [%s](%d files) deleted." % (directory, purge(directory));
print '--------------------';
if __name__=='__main__':
print 'start';
os.path.walk(root_dir, callback, 0);
print 'complete.';