Skip to content

Instantly share code, notes, and snippets.

@gmastrokostas
Created November 25, 2015 09:26
Show Gist options
  • Select an option

  • Save gmastrokostas/9abfc482c9fcf5d0c482 to your computer and use it in GitHub Desktop.

Select an option

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
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