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

只列出当前目录和子目录方法一

1、编辑脚本

1
2
3
4
5
[root@iZbp171r05i3piseee5kuaZ tmp] # vim /root/filelist.py 
#!/usr/bin/env python
import  os
for  root,dirs,files  in  os.walk( '/tmp' ):
  print  root


2、执行脚本和确认

1
2
3
4
5
6
7
8
9
10
11
12
13
[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、编辑脚本

1
2
3
4
5
6
[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、执行脚本和确认([]里面表示子目录)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[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、编辑脚本

1
2
3
4
5
6
7
[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、执行脚本和确认(第二个[]里面表示子目录,第三个[]里面表示文件)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[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、编辑脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/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、执行脚本和确认

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[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、编辑脚本

1
2
3
4
5
6
7
8
9
10
11
12
#!/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, οnerrοr=None, followlinks=False),我们一般只使用第一个参数。(topdown指明遍历的顺序)。
该方法对于每个目录返回一个三元组,(dirpath, dirnames, filenames)。第一个是路径,第二个是路径下面的目录,第三个是路径下面的非目录(对于windows来说也就是文件)

2、os.path.join(path1[, path2[, ...]])  #把目录和文件名合成一个路径




本文转自 sailikung 51CTO博客,原文链接:http://blog.51cto.com/net881004/2051910,如需转载请自行联系原作者


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值