Created
October 30, 2019 10:33
-
-
Save giasuddin90/6e08c00520589c1bcaf25b6b5cac0a60 to your computer and use it in GitHub Desktop.
Using python script find different pattern file from a directory and replace file name as a way we want.
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 re | |
| import glob, os | |
| def file_rename(dir, pattern, titlePattern): | |
| for pathAndFilename in glob.iglob(os.path.join(dir, pattern)): | |
| title, ext = os.path.splitext(os.path.basename(pathAndFilename)) | |
| replace_file = re.sub("[^a-zA-Z0-9_]+", "_", title) | |
| new_filename = (replace_file + ext ).lower() | |
| os.rename(pathAndFilename, | |
| os.path.join(dir, titlePattern + new_filename)) | |
| # os.path.join(dir, new_filename)) | |
| if __name__ == '__main__': | |
| file_rename("/home/giasuddin/Downloads/dir_name", "*.pdf", "file_") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment