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, onerror=None, followlinks=False),我们一般只使用第一个参数。(topdown指明遍历的顺序)。
该方法对于每个目录返回一个三元组,(dirpath, dirnames, filenames)。第一个是路径,第二个是路径下面的目录,第三个是路径下面的非目录(对于windows来说也就是文件)

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




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


相关文章
|
数据可视化 Linux iOS开发
Python脚本转EXE文件实战指南:从原理到操作全解析
本教程详解如何将Python脚本打包为EXE文件,涵盖PyInstaller、auto-py-to-exe和cx_Freeze三种工具,包含实战案例与常见问题解决方案,助你轻松发布独立运行的Python程序。
2603 2
|
JSON 算法 API
深度分析小红书城API接口,用Python脚本实现
小红书作为以UGC内容为核心的生活方式平台,其非官方API主要通过移动端抓包解析获得,涵盖内容推荐、搜索、笔记详情、用户信息和互动操作等功能。本文分析了其接口体系、认证机制及请求规范,并提供基于Python的调用框架,涉及签名生成、登录态管理与数据解析。需注意非官方接口存在稳定性与合规风险,使用时应遵守平台协议及法律法规。
|
JSON API 数据格式
深度分析大麦网API接口,用Python脚本实现
大麦网为国内领先演出票务平台,提供演唱会、话剧、体育赛事等票务服务。本文基于抓包分析其非官方接口,并提供Python调用方案,涵盖演出列表查询、详情获取及城市列表获取。需注意非官方接口存在稳定性风险,使用时应遵守平台规则,控制请求频率,防范封禁与法律风险。适用于个人学习、演出信息监控等场景。
|
11月前
|
监控 机器人 编译器
如何将python代码打包成exe文件---PyInstaller打包之神
PyInstaller可将Python程序打包为独立可执行文件,无需用户安装Python环境。它自动分析代码依赖,整合解释器、库及资源,支持一键生成exe,方便分发。使用pip安装后,通过简单命令即可完成打包,适合各类项目部署。
1873 68
|
JSON API 开发者
深度分析阿里妈妈API接口,用Python脚本实现
阿里妈妈是阿里巴巴旗下营销平台,提供淘宝联盟、直通车等服务,支持推广位管理、商品查询等API功能。本文详解其API调用方法,重点实现商品推广信息(佣金、优惠券)获取,并提供Python实现方案。
|
JSON API 数据安全/隐私保护
深度分析虾皮城API接口,用Python脚本实现
虾皮开放平台提供丰富的API接口,支持商品管理、订单处理及促销信息查询等功能。本文详解API认证机制与调用方法,基于Python实现商品价格及到手价获取方案,适用于电商数据分析与运营。
|
API 数据安全/隐私保护 开发者
深度分析苏宁API接口,用Python脚本实现
深度分析苏宁API接口,用Python脚本实现
|
前端开发 Shell API
深度分析58同城API接口,用Python脚本实现
58同城为国内知名分类信息平台,涵盖房产、招聘、二手车等多领域。本文基于网页抓包与解析,分享其非官方接口的Python实现方案,分析核心接口特性与反爬应对策略,适用于数据学习与信息聚合。注意:非官方接口存在风险,使用需遵守平台规则。
|
Python
python读写execle文件数据
python读写execle文件数据
228 0

推荐镜像

更多