如何获得循环阅读子目录并列出文件,然后返回到我正在工作的目录并列出其中的文件中的文件?

发布于 2025-01-30 17:56:47 字数 342 浏览 4 评论 0原文

我正在尝试找出如何在子目录中列出文件的返回到我正在工作的目录并继续查找文件

#!home/usr/env Python3中 的文件 导入操作系统 文件= [] pather = os.path 尽管 : 对于os.listdir()中的文件: 如果文件==“ for.py”或file.startswith(“。”): 继续 files.append(文件) 而OS.Path.isdir(file)==“ true”: OS.CHDIR(文件) 如果OS.Pathifile(文件): files.append(file)

print(files),

这是我到目前为止的代码,

如果您可以提供帮助,那真是太棒了

I'm trying to find out how to list files in a subdirectory the return to the directory I was working in and continue finding files

#!home/usr/env python3
import os
files = []
pather = os.path
while :
for file in os.listdir():
if file == "for.py" or file.startswith("."):
continue
files.append(file)
while os.path.isdir(file) == "True":
os.chdir(file)
if os.pathisfile(file):
files.append(file)

print(files)

That's my code so far

If you could please help that would be amazing

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

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

发布评论

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

评论(1

柠檬色的秋千 2025-02-06 17:56:48

这应该有效:

import os
files = [os.path.join(path, name).replace(os.getcwd(), "") for path, subdirs, files in os.walk(os.getcwd()) for name in files]

This should work:

import os
files = [os.path.join(path, name).replace(os.getcwd(), "") for path, subdirs, files in os.walk(os.getcwd()) for name in files]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文