python列出当前目录、子目录和文件的脚本
2017-12-18 22:23
357 查看
只列出当前目录和子目录方法一
1、编辑脚本
2、执行脚本和确认
只列出当前目录和子目录方法二
1、编辑脚本
2、执行脚本和确认([]里面表示子目录)
列出当前目录、子目录和文件方法一
1、编辑脚本
2、执行脚本和确认(第二个[]里面表示子目录,第三个[]里面表示文件)
列出当前目录、子目录和文件方法二
1、编辑脚本
2、执行脚本和确认
列出当前目录、子目录和文件方法三
1、编辑脚本
备注:
1、os.walk()原型为:os.walk(top, topdown=True, onerror=None, followlinks=False),我们一般只使用第一个参数。(topdown指明遍历的顺序)。
该方法对于每个目录返回一个三元组,(dirpath, dirnames, filenames)。第一个是路径,第二个是路径下面的目录,第三个是路径下面的非目录(对于windows来说也就是文件)
2、os.path.join(path1[, path2[, ...]]) #把目录和文件名合成一个路径
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 脚本,把当前目录及子目录下的 wav 音频文件转换为 flac 格式
- python递归目录子目录列出目录和文件
- Python递归列出目录中文件脚本及其匿名函数
- shell脚本仅列出当前目录下的文件及文件夹
- Python 获取当前脚本文件路径目录
- 如何列出当前目录下所有的JSP文件(含子目录)
- 编写一个Python程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径
- python-列出所有目录及子目录文件
- Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
- ls列出当前目录[包含子目录]的文件的详细信息
- Python_删除/创建指定目录及其下所有子目录的文件,该文件记录当前文件夹的内容
- DIR:列出当前目录及子目录下的所有文件
- Python:编写函数,能在当前目录以及子目录下查找文件名包含指定字符串的文件,并打印出完整路径
- Python 在当前目录以及其子目录下查找文件名包含指定字符串的文件,并打印出相对路径。
- Python 获取当前脚本文件路径目录
- 用python列出当前目录的.py文件
- Python列出当前目录下所有含有指定名称的文件(包括路径)
- Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
- Python_删除/创建指定目录及其下所有子目录的文件,该文件记录当前文件夹的内容
- Python实现删除当前目录下除当前脚本以外的文件和文件夹实例