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?


@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

22 Answered Questions

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

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 3289637 View
  • 3475 Score
  • 22 Answer
  • Tags:   python directory

10 Answered Questions

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

55 Answered Questions

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

26 Answered Questions

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

21 Answered Questions

15 Answered Questions

[SOLVED] What are metaclasses in Python?

44 Answered Questions

[SOLVED] Replacements for switch statement in Python?

56 Answered Questions

[SOLVED] Calling an external command in Python

11 Answered Questions

[SOLVED] How to substring a string in Python?

  • 2009-03-19 17:29:41
  • Joan Venge
  • 2524842 View
  • 1873 Score
  • 11 Answer
  • Tags:   python string

13 Answered Questions

[SOLVED] Find current directory and file's directory

  • 2011-02-28 01:51:21
  • John Howard
  • 2421748 View
  • 1834 Score
  • 13 Answer
  • Tags:   python directory

Sponsored Content