【问题标题】:print the directory for files in Python [duplicate]在Python中打印文件的目录[重复]
【发布时间】:2015-04-06 05:41:38
【问题描述】:
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 file


【解决方案1】:

我会试一试,希望对你有帮助。

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

【讨论】:

  • 谢谢,真的很有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多