如何在Python中列出目录的内容?

发布于 2024-08-31 07:17:16 字数 18 浏览 3 评论 0原文

不难,但我有心理障碍。

Can’t be hard, but I’m having a mental block.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

皇甫轩 2024-09-07 07:17:17

在 Python 3.4+ 中,您可以使用新的 pathlib package:

from pathlib import Path
for path in Path('.').iterdir():
    print(path)

Path.iterdir() 返回一个迭代器,可以轻松地将其转换为 list

contents = list(Path('.').iterdir())

In Python 3.4+, you can use the new pathlib package:

from pathlib import Path
for path in Path('.').iterdir():
    print(path)

Path.iterdir() returns an iterator, which can be easily turned into a list:

contents = list(Path('.').iterdir())
顾冷 2024-09-07 07:17:17

从 Python 3.5 开始,您可以使用 os.scandir。

不同之处在于它返回文件条目而不是名称。在某些操作系统(例如 Windows)上,这意味着您不必通过 os.path.isdir/file 来知道它是否是文件,这可以节省 CPU 时间,因为 stat在 Windows 中扫描 dir 时,code> 已经完成:

列出目录并打印大于 max_value 字节的文件的示例:(

for dentry in os.scandir("/path/to/dir"):
    if dentry.stat().st_size > max_value:
       print("{} is biiiig".format(dentry.name))

阅读我的基于性能的广泛答案 此处

Since Python 3.5, you can use os.scandir.

The difference is that it returns file entries not names. On some OSes like windows, it means that you don't have to os.path.isdir/file to know if it's a file or not, and that saves CPU time because stat is already done when scanning dir in Windows:

example to list a directory and print files bigger than max_value bytes:

for dentry in os.scandir("/path/to/dir"):
    if dentry.stat().st_size > max_value:
       print("{} is biiiig".format(dentry.name))

(read an extensive performance-based answer of mine here)

秋凉 2024-09-07 07:17:17

下面的代码将列出目录和目录中的文件。另一种是 os.walk

def print_directory_contents(sPath):
        import os                                       
        for sChild in os.listdir(sPath):                
            sChildPath = os.path.join(sPath,sChild)
            if os.path.isdir(sChildPath):
                print_directory_contents(sChildPath)
            else:
                print(sChildPath)

Below code will list directories and the files within the dir. The other one is os.walk

def print_directory_contents(sPath):
        import os                                       
        for sChild in os.listdir(sPath):                
            sChildPath = os.path.join(sPath,sChild)
            if os.path.isdir(sChildPath):
                print_directory_contents(sChildPath)
            else:
                print(sChildPath)
美人如玉 2024-09-07 07:17:16
import os
os.listdir("path") # returns list
import os
os.listdir("path") # returns list
童话 2024-09-07 07:17:16

一种方式:

import os
os.listdir("/home/username/www/")

另一种方式

glob.glob("/home/username/www/*")

此处找到示例

上面的 glob.glob 方法不会列出隐藏文件。

由于我几年前最初回答了这个问题,pathlib 已添加到 Python 中。现在,我列出目录的首选方法通常涉及 Path 对象上的 iterdir 方法:

from pathlib import Path
print(*Path("/home/username/www/").iterdir(), sep="\n")

One way:

import os
os.listdir("/home/username/www/")

Another way:

glob.glob("/home/username/www/*")

Examples found here.

The glob.glob method above will not list hidden files.

Since I originally answered this question years ago, pathlib has been added to Python. My preferred way to list a directory now usually involves the iterdir method on Path objects:

from pathlib import Path
print(*Path("/home/username/www/").iterdir(), sep="\n")
猫瑾少女 2024-09-07 07:17:16

如果需要递归,可以使用 os.walk:

import os
start_path = '.' # current directory
for path,dirs,files in os.walk(start_path):
    for filename in files:
        print os.path.join(path,filename)

os.walk can be used if you need recursion:

import os
start_path = '.' # current directory
for path,dirs,files in os.walk(start_path):
    for filename in files:
        print os.path.join(path,filename)
靑春怀旧 2024-09-07 07:17:16

glob.globos.listdir 就可以了。

glob.glob or os.listdir will do it.

姐不稀罕 2024-09-07 07:17:16

os 模块 处理所有这些事情。

os.listdir(路径)

返回一个列表,其中包含路径给定目录中的条目名称。
该列表的顺序是任意的。它不包括特殊条目“.”和
'..',即使它们存在于目录中。

可用性:Unix、Windows。

The os module handles all that stuff.

os.listdir(path)

Return a list containing the names of the entries in the directory given by path.
The list is in arbitrary order. It does not include the special entries '.' and
'..' even if they are present in the directory.

Availability: Unix, Windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文