打印Python文件的目录

23 浏览
0 Comments

打印Python文件的目录

这个问题已经有了答案:如何列出目录中的所有文件?

import os 
for dirname, dirnames, filenames in os.walk('# I will have a directory here'):
    for filename in filenames:
        print (os.path.join(dirname, filename))

这是在Python中为某些文件打印目录的意思,但除此之外我知道的很少...谢谢...有其他页面有代码,但它没有完全以“呆瓜指南”的方式解释清楚...所以我有点困惑。感谢您的帮助.. 🙂

admin 更改状态以发布 2023年5月22日
0
0 Comments

我会试试看,希望这能帮到你。

import os 
#   vvvvvvv directory name of the current position in the walk (eg, /var then /var/log then /var/log/apache2, etc)
#            vvvvvvvv list (array) of sub directories
#                      vvvvvvvvv list (array) of files
for dirname, dirnames, filenames in os.walk('# I will have a directory here'): # loops through the directories list
    for filename in filenames: #loops through the files returned by the previous for loop
      print (os.path.join(dirname, filename)) # join the directory and the filename returning something like /var/log/dmesg.log

0