Created
November 25, 2015 09:26
-
-
Save gmastrokostas/9abfc482c9fcf5d0c482 to your computer and use it in GitHub Desktop.
Python – Change one specific extension type of files in a directory that contains multiple types of extensions
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 | |
| import re | |
| src_drct='/home/gmastrokostas/tmp' | |
| for files in os.listdir(src_drct): | |
| if files.endswith('.txt'): | |
| oldF = os.path.join(src_drct, files) | |
| #midF = re.split(r'\.', files)#This works too. | |
| midF = os.path.splitext(files) #It creates a list and splits the name of the file from the extension. | |
| newF = oldF.replace('.txt', '.py')#Simple replacement. | |
| out = os.rename(oldF, newF) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment