关于帮助文件:请说明本代码在Python

Please help explain this code in Python

本问题已经有最佳答案,请猛点这里访问。
1
2
3
4
5
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中打印一些文件的目录。但除了我所知甚少…谢谢。。。还有其他一些页面有这个代码,但它没有在"假人指南"中完全解释。所以我有点困惑。感谢您的帮助……)


我试试看,希望能有所帮助。

1
2
3
4
5
6
7
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