如何用Python实现目录遍历
1个回答
展开全部
1. 基本实现
[root@localhost ~]# cat dirfile.py
import os
path='/tmp'
for dirpath,dirnames,filenames in os.walk(path):
for file in filenames:
fullpath=os.path.join(dirpath,file)
print fullpath
执行结果如下:
[root@localhost ~]# python dirfile.py
/tmp/yum.log
/tmp/pulse-3QSA3BbwpQ49/pid
/tmp/pulse-3QSA3BbwpQ49/native
/tmp/.esd-0/socket
[root@localhost ~]# cat dirfile.py
import os
path='/tmp'
for dirpath,dirnames,filenames in os.walk(path):
for file in filenames:
fullpath=os.path.join(dirpath,file)
print fullpath
执行结果如下:
[root@localhost ~]# python dirfile.py
/tmp/yum.log
/tmp/pulse-3QSA3BbwpQ49/pid
/tmp/pulse-3QSA3BbwpQ49/native
/tmp/.esd-0/socket
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询