简体   繁体   English

无法使用python在Windows中递归遍历目录

[英]Not able to recursively walk directory in windows using python

I am trying to recursively walk a dir in windows using python. 我试图使用python在Windows中递归遍历目录。

def create_folder_structure():
    for root, dirs, files in os.walk(r'C:\Users\patela28\Desktop\jira'):
        # print(folder_path)
        print(dirs)

create_folder_structure()

The above code works. 上面的代码有效。 But not the below one. 但不是以下一项。

def create_folder_structure():
    print(folder_path)
    for root, dirs, files in os.walk(folder_path):
        print(dirs)

create_folder_structure()

and I get the following output: 我得到以下输出:

C:\Users\patela28\Desktop\unittest>python unittest.py SMC-11883

C:\Users\patela28\Desktop\jira

C:\Users\patela28\Desktop\unittest>

Don't know why this is happening. 不知道为什么会这样。

Try this: 尝试这个:

import os

def test():
    path = 'C:/Users/xxx/Downloads'
    for root, dirs, files in os.walk(path):
        return dirs
print(test())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM