gpt4 book ai didi

python-2.7 - 如何递归列出所有文件夹和文件?

转载 作者:行者123 更新时间:2023-12-04 20:09:26 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Browse files and subfolders in Python

(6 个回答)


7年前关闭。




如何获取文件夹和文件 包括 python中子目录的文件/文件夹?
我需要每个文件/文件夹的绝对路径。

我想重命名所有文件夹和文件。所以我必须先重命名文件夹。

folder
-- file
-- folder1
---- folder1.1
------ file
------ folder1.1.1
-------- file
-- folder2
---- ...

最佳答案

我快速环顾四周,发现它很容易。来自 Sven Marnach :

You can us os.walk() to recursively iterate through a directory and all its subdirectories:

for root, dirs, files in os.walk(path):
for name in files:
if name.endswith((".html", ".htm")):
# whatever

To build a list of these names, you can use a list comprehension:

htmlfiles = [os.path.join(root, name)
for root, dirs, files in os.walk(path)
for name in files
if name.endswith((".html", ".htm"))]

关于python-2.7 - 如何递归列出所有文件夹和文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20578851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com