Skip to content

Instantly share code, notes, and snippets.

@P7h
Last active April 15, 2016 02:14
Show Gist options
  • Select an option

  • Save P7h/44bb9456121dab26804c to your computer and use it in GitHub Desktop.

Select an option

Save P7h/44bb9456121dab26804c to your computer and use it in GitHub Desktop.
Renaming files in many sub-folders. Requirement: There is a huge directory list which has many sub-directories and one file in each sub-directory, which has to be renamed as sub-directory name with the original extension. Last sub-directory name is always 8 chars. After renaming is done, it has to be moved to a different folder. Wrote a small Py…
import os
# Current working directory where files and folders are present.
rootDir = os.getcwd()
# Destination directory to copy files.
destinationDir = 'D:/GBR/'
# Walk thru the directories in this root directory.
for dirName, subdirList, fileList in os.walk(rootDir):
# There will always be only one file in each sub-directory.
# Last sub-directory is of 8 chars only.
for fname in fileList:
print fname
# Rename the only file present in the last sub-directory [and retain the original extension] and move it to the desination directory.
os.rename(dirName + "\\" + fname, destinationDir + dirName[-8:] + ".geo.json")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment