2015-02-06 58 views
-4
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中打印一些文件的目录。但除此之外,我知道的一点点...谢谢...还有其他的代码页面,但它并没有完全在'傻瓜式'指南中解释它。所以我有点困惑。感谢您的帮助.. :)请帮忙在Python中解释这段代码

+0

请缩进您的代码。 。简单地解释你想要实现的部分? – 2015-02-06 06:20:56

+1

你有关于文档的具体问题吗? https://docs.python.org/3/library/os.html#os.walk – 2015-02-06 06:22:11

+0

BREH,请访问https://codereview.stackexchange.com/,然后提出这个问题。 – EgMusic 2017-10-12 16:50:41

回答

3

我会给它一个镜头,希望这有助于。

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

谢谢,这真的很有帮助 – Athullya 2015-02-06 06:31:25