Rename All Files in a Directory Using Python

MMiller picture MMiller · Nov 3, 2014 · Viewed 10.1k times · Source

I have a directory containing a number of files with this format:

1 or 2 numbers_S followed by 1 or 2 numbers_L001_R1 or R2_001.fastq

Examples: 1_S1_L001_R1_001.fastq or 14_S14_L001_R2_001.fastq

I want the file names to be like this: 1_R1.fastq 14_R2.fastq

I have figured out the regexp that reflects the file names and can successfully do the search and replace within TextWrangler. Below is the regexp that I came up with:

Search: (\d+)\wS\d+\wL001\w(R\d)\w001(\.fastq)
Replace: \1_\2\3 (or $1_$2$3 depending on the program)

However, I would like to know how to batch rename the files using a simple Python script. I would appreciate any advice.

Thank you!

Answer

JBernardo picture JBernardo · Nov 3, 2014

You can do something like this

 import glob, re, os

 for filename in glob.glob('/some/dir/*.fastq'):
     new_name = re.sub(pattern, r'\1_\2\3', filename)
     os.rename(filename, new_name)