I need to rename many files with the following format:
509938_OPS001_ACTCGCTA-TCGACTAG_L001_R1_001.fastq.gz
I've tested my regex:
\d+\w([OPS]+\d+)[_]\w+-\w+[_](\d+)(\.fastq\.gz)
I've tried many versions of the following script, but each throws a syntax error following re.sub.
import glob, re, os
for filename in glob.glob('some/dir/*.fastq.gz'):
new_name = re.sub(\d+\w([OPS]+\d+)[_]\w+-\w+[_](\d+)(\.fastq\.gz), r'\1_\2\3', filename)
os.rename(filename, new_name)
$python fastq_rename.py
File "fastq_rename.py", line 6
new_name = re.sub(\d+\w([OPS]+\d+)[_]\w+-\w+[_](\d+)(\.fastq\.gz)), r'\1_\2\3', filename)
^
SyntaxError: unexpected character after line continuation character
Presuming this has to do with an unescaped backslash, I've enclosed the regex with r' ', and this avoids the error, but does not change filenames.