By Laurie Bamber


2018-09-14 15:57:16 8 Comments

I'm using the following function to remove a specific string pattern from files in a directory:

import os
for filename in os.listdir(path):
   os.rename(filename, filename.replace(r'^[A-Z]\d\d\s-\s[A-Z]\d\d\s-\s$', ''))

The pattern is as follows, where A is any capital letter, and # is any number between 0-9:

A## - A## -

My regex matches this format on regex101. When I run the above function, it completes without error, however no directory names change. Where am I going wrong?

2 comments

@Chillie 2018-09-14 15:59:59

replace string method does not support regular expressions.

You need to import the re module and use its sub method.

So your code might look like this:

import os
import re
for filename in os.listdir(path):
   os.rename(filename, re.sub(r'^[A-Z]\d\d\s-\s[A-Z]\d\d\s-\s', '', filename))

But don't forget about flags and such.

Edit: Removed $ from the pattern as the filenames don't end there.

@Laurie Bamber 2018-09-14 16:05:57

Odd, I've tried this but it's still not working for some reason, it completes without error however nothings changed.

@Chillie 2018-09-14 16:07:42

@LaurieBamber could you give us a couple sample filenames?

@Laurie Bamber 2018-09-14 16:08:17

File 1: 'S01 - E01 - Something Here', File 2: 'S01 - E02 - Something Different'.... File k: 'S07 - E06 - Something Different'

@Chillie 2018-09-14 16:09:32

@LaurieBamber You have the string end marker ($) in your pattern, but the file name does not end there, so it doesn't match. Just remove the $ from your pattern.

@Laurie Bamber 2018-09-14 16:10:07

Gentleman, thats done it. Thanks for the help.

@Chillie 2018-09-14 16:15:51

@LaurieBamber Happy to help. Don't forget to mark the question as solved (answer as accepted). =)

@mad_ 2018-09-14 16:03:28

import re
filename='A11 - A22 - '#A## - A## -
re.sub(filename,r'^[A-Z]\d\d\s-\s[A-Z]\d\d\s-\s', '')

@Laurie Bamber 2018-09-14 16:07:47

Thanks, this still isn't working for some reason with my code though.

@mad_ 2018-09-14 16:09:35

can you print the output after executing the above code?

@Laurie Bamber 2018-09-14 16:11:17

My mistake, as pointed out by Chillie I was incorrectly using the $ symbol to finish the pattern. Thanks for the help anyway.

Related Questions

Sponsored Content

43 Answered Questions

[SOLVED] How to replace all occurrences of a string in JavaScript?

52 Answered Questions

[SOLVED] Calling an external command in Python

16 Answered Questions

[SOLVED] What are metaclasses in Python?

25 Answered Questions

[SOLVED] How can I safely create a nested directory in Python?

32 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 2561418 View
  • 2916 Score
  • 32 Answer
  • Tags:   python directory

48 Answered Questions

[SOLVED] Replacements for switch statement in Python?

  • 2008-09-13 00:36:30
  • Michael Schneider
  • 1229429 View
  • 1493 Score
  • 48 Answer
  • Tags:   python

23 Answered Questions

14 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

10 Answered Questions

[SOLVED] Is there a way to substring a string in Python?

  • 2009-03-19 17:29:41
  • Joan Venge
  • 2125930 View
  • 1638 Score
  • 10 Answer
  • Tags:   python string

25 Answered Questions

[SOLVED] What is the difference between @staticmethod and @classmethod in Python?

Sponsored Content