How to use the dir/s command in Python?

后端 未结 5 2073
[愿得一人]
[愿得一人] 2021-01-13 07:13

Background

I use the command dir/s in batch files all the time. But, I am unable to call this using python. NOTE: I am

5条回答
  •  庸人自扰
    2021-01-13 07:53

    I finally found the answer. To list all directories in a directory (e.g. D:\\, C:\\) on needs to first import the os module.

    import os
    

    Then, they need to say that they want to list everything. Within that, they need to make sure that the output is printed.

    for top, dirs, files in os.walk('D:\\'):
        for nm in files:       
            print os.path.join(top, nm)
    

    That was how I was able to solve it. Thanks to this.

提交回复
热议问题