Skip to content

Instantly share code, notes, and snippets.

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

  • Save gmastrokostas/31a794fc24c32f7cd5b0 to your computer and use it in GitHub Desktop.

Select an option

Save gmastrokostas/31a794fc24c32f7cd5b0 to your computer and use it in GitHub Desktop.
Python – Search for specific types of files and rename the files or part of the files
import os
import re
src_drct='/home/gmastrokostas/tmp'
for files in os.listdir(src_drct):
if files.endswith('.txt'): #Select all files with the .txt ext
oldF = os.path.join(src_drct, files) #Join full path with files found
midF = re.split(r'\_', files) #split files that contain underscore
newF = oldF.replace('_01012015', '_01012014') #create the replacement var
out = os.rename(oldF, newF) #start renaming.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment