请帮助用Python解释这段代码

时间:2015-02-06 06:19:48

标签: python file

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 个答案:

答案 0 :(得分:5)

我会试一试,希望这有帮助。

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