【问题标题】:How to get a list of all files in directory using python.如何使用python获取目录中所有文件的列表。
【发布时间】:2014-09-15 08:40:28
【问题描述】:

我正在使用记事本++的python插件,需要能够打印目录中的所有文件。基本上我需要在命令提示符下运行 dir 之类的东西。

有没有办法做到这一点?

【问题讨论】:

  • 你的意思是os.listdir('.')?
  • 如果通过“打印输出”,你的意思是格式化的东西,还有一堆额外的列,等等,就像dir,那么你需要在每一个上使用os.stat并自己格式化列——当然,您也可以使用subprocess.check_output 来运行命令提示符的dir 命令。

标签: python notepad++


【解决方案1】:

您可以使用os.listdir() 函数获取目录中的文件列表。

【讨论】:

    【解决方案2】:

    所以,这里是如何将一个目录中的所有文件及其所有子目录中的一个列表,打印出该列表。

    注意:如何“走路”在这里找到:

    concatenate the directory and file name

    # Task: Get a printout of all the files in a directory.
    
    import os
    
    # The directory that we are interested in
    myPath = "/users/george/documents/"
    
    # All the file paths will be stored in this list
    filesList= []
    
    for path, subdirs, files in os.walk(myPath):
        for name in files:
            filesList.append(os.path.join(path, name))
    
    for i in filesList:
        print (str(i))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 2010-12-01
      • 2017-01-21
      • 2010-11-30
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      相关资源
      最近更新 更多