遵循嵌套的目录结构直到结束

2024-04-24 12:03:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些目录包含一些其他目录,在最底层,包含一堆csv文件,例如(文件夹)a->;b->;c->;(csv文件)。通常每个级别只有一个文件夹。当我处理一个目录时,我怎样才能遵循这个结构直到最后得到csv文件?我在想也许是一个递归的解决方案,但我认为可能有更好的方法来做到这一点。我用的是python。希望我是清楚的。在


Tags: 文件csv方法gt目录文件夹解决方案级别
1条回答
网友
1楼 · 发布于 2024-04-24 12:03:00

^{}包有一个^{}函数,它将精确地执行您所需的操作:

for current_path, directory, files in walk("/some/path"):
    # current_path is the full path of the directory we are currently in
    # directory is the name of the directory
    # files is a list of file names in this directory

可以使用^{}'s派生每个文件的完整路径(如果需要)。在

或者,您可能会发现^{}模块对您更有用:

^{pr2}$

相关问题 更多 >