Python program to traverse directories and read file information

后端 未结 3 2138
感动是毒
感动是毒 2021-02-19 16:23

I\'m just getting started with Python but already have found it much more productive than Bash shell scripting.

I\'m trying to write a Python script that will traverse e

相关标签:
3条回答
  • 2021-02-19 16:54

    Try it

    import os

    for item in os.walk(".", "*"):

         print item 
    
    0 讨论(0)
  • Try

    info = []
    for path, dirs, files in os.walk("."):
        info.extend(FileInfo(filename, path) for filename in files)
    

    or

    info = [FileInfo(filename, path)
            for path, dirs, files in os.walk(".")
            for filename in files]
    

    to get a list of one FileInfo instance per file.

    0 讨论(0)
  • 2021-02-19 17:15

    I'd use os.walk doing the following:

    def getInfos(currentDir):
        infos = []
        for root, dirs, files in os.walk(currentDir): # Walk directory tree
            for f in files:
                infos.append(FileInfo(f,root))
        return infos
    
    0 讨论(0)
提交回复
热议问题