如何使用Python列出目录

gwa*_*ito 8 python directory list

可能的重复:
如何在python中列出目录的所有文件

如何使用 python 列出所有目录内容并将其添加到列表中?

gwa*_*ito 5

使用 Python,有很多方法可以做到这一点,但这是我所知道的最简单的一种。

请检查注释以了解输出的细节。

from os import listdir
list = listdir("C:\Users\Hello\World\Python Programs")
print list #Outputs the whole list
print len(list) #Checks for the length of the list
print list[26] #Outputs a specific element in the list
Run Code Online (Sandbox Code Playgroud)

祝你好运!

  • 可能要小心路径中的反斜杠,因为它们与字符串转义字符冲突。 (2认同)
  • 嗯……如果您同时找出答案,我们鼓励您回答自己的问题,但是……您为什么要表现得好像你们是两个人?(例如,带有“祝你好运!”) (2认同)