python os.walk()和os.listdir()

本部分内容的组织关系:python3.6.5文档 python标准库 16 通用操作系统服务 16.1.5 文件和目录
对于官方文档的学习可以和每周一个python模块相互协作着进行。
还可以查看如何系统的学习python标准库

os.listdir(path = ‘.’)

入口为path给定的目录,函数返回一个列表,列表顺序是随机的,并且不包括入口’.’,’..’,即使它们在目录中存在。

os.listdir(path=’.’)
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries ‘.’ and ‘..’ even if they are present in the directory.

os.walk(top, topdown=True, οnerrοr=None, followlinks=False)

遍历顶级目录下的每个目录,每个目录返回一个三元组(路径,目录名,文件名)。默认是自上而下遍历,将topdown改为False则自下而上遍历。目录名是路径下的子目录名列表,排除掉’.’,’..’。注意,这些名字不包含路径。如果想得到全路径,可以使用os.join(dirpath, name)。

os.walk(top, topdown=True, οnerrοr=None, followlinks=False)
Generate the file names in a directory tree by walking the tree either top-down or bottom-up.
dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding ‘.’ and ‘..’). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).
“`

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值