Last active
April 15, 2016 02:14
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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