您的位置:首页 > 编程语言 > Python开发

python列出当前目录、子目录和文件的脚本

2017-12-18 22:23 357 查看
只列出当前目录和子目录方法一

1、编辑脚本
[root@iZbp171r05i3piseee5kuaZ tmp]# vim /root/filelist.py
#!/usr/bin/env python
import os
for root,dirs,files in os.walk('/tmp'):
print root


2、执行脚本和确认

[root@iZbp171r05i3piseee5kuaZ tmp]# python /root/filelist.py
/tmp
/tmp/gxmdir
/tmp/gxmdir/ddd
/tmp/csdir
/tmp/.ICE-unix
[root@iZbp171r05i3piseee5kuaZ tmp]# tree -d
.
├── csdir
└── gxmdir
└── ddd

3 directories


只列出当前目录和子目录方法二
1、编辑脚本
[root@iZbp171r05i3piseee5kuaZ tmp]# vim /root/filelist.py
#!/usr/bin/env python
import os
for root,dirs,files in os.walk('/tmp'):
print root
print dirs


2、执行脚本和确认([]里面表示子目录)
[root@iZbp171r05i3piseee5kuaZ tmp]# python /root/filelist.py
/tmp
['gxmdir', 'csdir', '.ICE-unix']
/tmp/gxmdir
['ddd']
/tmp/gxmdir/ddd
[]
/tmp/csdir
[]
/tmp/.ICE-unix
[]
[root@iZbp171r05i3piseee5kuaZ tmp]# tree -d
.
├── csdir
└── gxmdir
└── ddd

3 directories


列出当前目录、子目录和文件方法一
1、编辑脚本
[root@iZbp171r05i3piseee5kuaZ tmp]# vim /root/filelist.py
#!/usr/bin/env python
import os
for root,dirs,files in os.walk('/tmp'):
print root
print dirs
print files


2、执行脚本和确认(第二个[]里面表示子目录,第三个[]里面表示文件)
[root@iZbp171r05i3piseee5kuaZ tmp]# python /root/filelist.py
/tmp
['gxmdir', 'csdir', '.ICE-unix']
['mqm_status.txt', '.s.PGSQL.5432', 'zapache-9009-http___localhost_99_server-status_auto.cache', 'zapache-9009-http___localhost_99_server-status_auto.ts', '.s.PGSQL.5432.lock', 'Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>', 'tcp_status.txt', 'dspam.7z', 'smtp_monitor-stderr---supervisor-8onXRl.log', 'dspam.csv']
/tmp/gxmdir
['ddd']
['2222', '1111']
/tmp/gxmdir/ddd
[]
['5555']
/tmp/csdir
[]
['3333', '4444']
/tmp/.ICE-unix
[]
[]

[root@iZbp171r05i3piseee5kuaZ tmp]# tree -d
.
├── csdir
└── gxmdir
└── ddd

3 directories


列出当前目录、子目录和文件方法二
1、编辑脚本
#!/usr/bin/env python
import os
for root,dirs,files in os.walk('/tmp'):
for name in files:
print (os.path.join(root,name))
为什么files要再一次for循环列出来呢?因为列出来的格式是这样的,好用于os.path.join方法:
单独print name看看:
mqm_status.txt
.s.PGSQL.5432
zapache-9009-http___localhost_99_server-status_auto.cache
zapache-9009-http___localhost_99_server-status_auto.ts
.s.PGSQL.5432.lock
Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>
tcp_status.txt
dspam.7z
smtp_monitor-stderr---supervisor-8onXRl.log
dspam.csv
2222
1111
5555
3333
4444

没列出来的格式是这样的,不方便用于os.path.join方法:
单独print files看看:
['mqm_status.txt', '.s.PGSQL.5432', 'zapache-9009-http___localhost_99_server-status_auto.cache', 'zapache-9009-http___localhost_99_server-status_auto.ts', '.s.PGSQL.5432.lock', 'Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>', 'tcp_status.txt', 'dspam.7z', 'smtp_monitor-stderr---supervisor-8onXRl.log', 'dspam.csv']
['2222', '1111']
['5555']
['3333', '4444']
[]


2、执行脚本和确认
[root@iZbp171r05i3piseee5kuaZ tmp]# python /root/filelist.py
/tmp/mqm_status.txt
/tmp/.s.PGSQL.5432
/tmp/zapache-9009-http___localhost_99_server-status_auto.cache
/tmp/zapache-9009-http___localhost_99_server-status_auto.ts
/tmp/.s.PGSQL.5432.lock
/tmp/Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>
/tmp/tcp_status.txt
/tmp/dspam.7z
/tmp/smtp_monitor-stderr---supervisor-8onXRl.log
/tmp/dspam.csv
/tmp/gxmdir/2222
/tmp/gxmdir/1111
/tmp/gxmdir/ddd/5555
/tmp/csdir/3333
/tmp/csdir/4444

[root@iZbp171r05i3piseee5kuaZ tmp]# tree -d
.
├── csdir
└── gxmdir
└── ddd

3 directories


列出当前目录、子目录和文件方法三
1、编辑脚本
#!/usr/bin/env python
import os

def scanfile(path):
filelist = os.listdir(path)
allfile = []
for filename in filelist:
filepath = os.path.join(path,filename)
if os.path.isdir(filepath):    #如果是目录,则执行函数。
scanfile(filepath)
print filepath      #如果不是目录,则直接打印filepath文件路径。
allfile = scanfile('/root/')

备注:
1、os.walk()原型为:os.walk(top, topdown=True, onerror=None, followlinks=False),我们一般只使用第一个参数。(topdown指明遍历的顺序)。
该方法对于每个目录返回一个三元组,(dirpath, dirnames, filenames)。第一个是路径,第二个是路径下面的目录,第三个是路径下面的非目录(对于windows来说也就是文件)
2、os.path.join(path1[, path2[, ...]]) #把目录和文件名合成一个路径
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 脚本
相关文章推荐