How to use glob() to find files recursively?

前端 未结 28 1831
天涯浪人
天涯浪人 2020-11-21 22:54

This is what I have:

glob(os.path.join(\'src\',\'*.c\'))

but I want to search the subfolders of src. Something like this would work:

<
28条回答
  •  遥遥无期
    2020-11-21 23:08

    This is a working code on Python 2.7. As part of my devops work, I was required to write a script which would move the config files marked with live-appName.properties to appName.properties. There could be other extension files as well like live-appName.xml.

    Below is a working code for this, which finds the files in the given directories (nested level) and then renames (moves) it to the required filename

    def flipProperties(searchDir):
       print "Flipping properties to point to live DB"
       for root, dirnames, filenames in os.walk(searchDir):
          for filename in fnmatch.filter(filenames, 'live-*.*'):
            targetFileName = os.path.join(root, filename.split("live-")[1])
            print "File "+ os.path.join(root, filename) + "will be moved to " + targetFileName
            shutil.move(os.path.join(root, filename), targetFileName)
    

    This function is called from a main script

    flipProperties(searchDir)
    

    Hope this helps someone struggling with similar issues.

提交回复
热议问题