转载
python中os.walk是一个简单易用的文件、目录遍历器,可以帮助我们高效的处理文件、目录方面的事情。
1.载入
要使用os.walk,首先要载入该函数
可以使用以下两种方法
import os
from os import walk
2.使用
os.walk的函数声明为:
walk(top, topdown=True, onerror=None, followlinks=False)
举个例子
如果我们有如下的文件结构:
a -> b -> 1.txt, 2.txt
c -> 3.txt
d ->
4.txt
5.txt
for (root, dirs, files) in os.walk('a'):
#第一次运行时,当前遍历目录为 a
所以 root == 'a'
dirs == [ 'b', 'c', 'd']
files == [ '4.txt', '5.txt']
。。。
# 接着遍历 dirs 中的每一个目录
b: root = 'a\\b'
dirs = []
files = [ '1.txt', '2.txt']
# dirs为空,返回
# 遍历c
c: root = 'a\\c'
dirs = []
files = [ '3.txt' ]
PS : 如果想获取文件的全路径,只需要
for f in files:
path = os.path.join(root,f)
# 遍历d
d: root = 'a\\b'
dirs = []
files = []
遍历完毕,退出循环
3.简单的例子
保持目录 a 的目录结构,在 b 中创建对应的文件夹,并把a中所有的文件加上后缀 _bak
import os
Root = 'a'
Dest = 'b'
for (root, dirs, files) in os.walk(Root):
new_root = root.replace(Root, Dest, 1)
if not os.path.exists(new_root):
os.mkdir(new_root)
for d in dirs:
d = os.path.join(new_root, d)
if not os.path.exists(d):
os.mkdir(d)
for f in files:
# 把文件名分解为 文件名.扩展名
# 在这里可以添加一个 filter,过滤掉不想复制的文件类型,或者文件名
(shotname, extension) = os.path.splitext(f)
# 原文件的路径
old_path = os.path.join(root, f)
new_name = shotname + '_bak' + extension
# 新文件的路径
new_path = os.path.join(new_root, new_name)
try:
# 复制文件
open(new_path, 'wb').write(open(old_path, 'rb').read())
except IOError as e:
print(e)
作者:MikuLovely
链接:https://www.jianshu.com/p/bbad16822eab
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。