欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

【python初级】os.listdir返回目录中包含的文件以及文件夹的列表

时间:2023-08-12
【python初级】os.listdir返回目录中包含的文件以及文件夹的列表

背景示例 背景

os.listdir(path)返回一个列表,该列表中包含path目录下的文件以及文件夹。

查看python官方的说明:

import oshelp(os.listdir)

运行返回:

Help on built-in function listdir in module nt:listdir(path=None) Return a list containing the names of the files in the directory. path can be specified as either str or bytes、 If path is bytes, the filenames returned will also be bytes; in all other circumstances the filenames returned will be str. If path is None, uses the path='.'. On some platforms, path may also be specified as an open file descriptor; the file descriptor must refer to a directory. If this functionality is unavailable, using it raises NotImplementedError. The list is in arbitrary order、 It does not include the special entries '.' and '..' even if they are present in the directory.

示例

简单的演示os.listdir(path)的使用:

# jn10010537import ospath="./test"result=os.listdir(path)print(">>>result:",result)

运行如下:

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。