python递归打印某个目录的内容(实例讲解)
作者:jingxian 时间:2021-02-13 00:54:27
以下函数列出某个目录下(包括子目录)所有文件,本随笔重点不在于递归函数的实现,这是一个很简单的递归,重点在于熟悉Python 库os以及os.path一些函数的功能和用法。
1. os.listdir(path): 列出path下所有内容(包括文件和目录,不包括.和..)
2. os.path.join(path1,path2,path3...): 拼接目录,例如将'home','test'拼接成'home/test/'
3. os.path.isdir(path): 判断path是否为目录
代码如下:
#!/usr/bin/python
import os
def print_dir(sPath):
for sChild in os.listdir(sPath):
sChildPath = os.path.join(sPath,sChild)
if os.path.isdir(sChildPath):
print_dir(sChildPath)
else:
print sChildPath
来源:http://www.cnblogs.com/fun8632/p/7449111.html
标签:python,递归,打印,目录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
远古幻想ICON 1套+创作思路
2007-09-30 20:33:00
![](https://img.aspxhome.com/file/UploadPic/20079/30/2007930203644880s.jpg)
Mootools 1.2教程(7)——设置和获取样式表属性
2008-11-25 13:48:00
python2.7 json 转换日期的处理的示例
2021-02-10 12:39:50
通过Py2exe将自己的python程序打包成.exe/.app的方法
2021-07-05 11:05:55
![](https://img.aspxhome.com/file/2023/6/71306_0s.jpg)
python基础pandas的drop()用法示例详解
2023-06-11 15:11:39
![](https://img.aspxhome.com/file/2023/3/93843_0s.png)
对Python使用mfcc的两种方式详解
2023-06-04 14:56:52
![](https://img.aspxhome.com/file/2023/0/67810_0s.jpg)
python 移动图片到另外一个文件夹的实例
2022-09-17 07:56:14
sqlserver 导出插入脚本代码
2012-01-29 18:04:43
python 中的np.zeros()和np.ones()函数详解
2023-01-14 10:23:58
基于Python对象引用、可变性和垃圾回收详解
2022-03-12 17:11:49
![](https://img.aspxhome.com/file/2023/8/107648_0s.jpg)
微信小程序利用Canvas绘制图片和竖排文字详解
2023-08-20 02:35:43
![](https://img.aspxhome.com/file/2023/2/56022_0s.jpg)
Python包资源下载路径报404解决方案
2021-01-22 15:38:35
![](https://img.aspxhome.com/file/2023/4/76744_0s.png)
Python+Opencv识别两张相似图片
2022-11-07 09:12:55
![](https://img.aspxhome.com/file/2023/0/97070_0s.jpg)
浅谈tensorflow 中tf.concat()的使用
2023-07-21 20:24:08
python-docx 页面设置详解
2021-12-06 03:42:02
![](https://img.aspxhome.com/file/2023/1/76081_0s.png)
Python之Web框架Django项目搭建全过程
2022-03-21 03:06:08
![](https://img.aspxhome.com/file/2023/3/93853_0s.png)
DOM 精简知识教程
2013-07-18 07:28:33
![](https://img.aspxhome.com/file/UploadPic/20071/200713110268848s.gif)
python实现微信定时每天和女友发送消息
2022-08-04 09:43:54
![](https://img.aspxhome.com/file/2023/6/100946_0s.jpg)
Python实现单项链表的最全教程
2021-12-21 09:45:26
![](https://img.aspxhome.com/file/2023/0/87890_0s.png)
浅析PyTorch中nn.Module的使用
2021-10-29 14:04:53