Marin
Marin

Reputation: 1121

Python Regex or Filename Function

Question about rename file name in folder. My file name looks like this:

EPG CRO 24 Kitchen 09.2013.xsl

With name space between, and I used code like this:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Remove whitespace from files where EPG named with space " " replace with "_"
for filename in os.listdir("."):
    if filename.find("2013|09 ") > 0:
        newfilename = filename.replace(" ","_")
        os.rename(filename, newfilename)

With this code I removed white space, but how can I remove date, from file name so it can look like this: EPG_CRO_24_Kitche.xsl. Can you give me some solution about this.

Upvotes: 0

Views: 365

Answers (4)

Brandon W. King
Brandon W. King

Reputation: 208

Regex

As utdemir was eluding to, regular expressions can really help in situations like these. If you have never been exposed to them, it can be confusing at first. Checkout https://www.debuggex.com/r/4RR6ZVrLC_nKYs8g for a useful tool that helps you construct regular expressions.

Solution

An updated solution would be:

import re

def rename_file(filename):
  if filename.startswith('EPG') and ' ' in filename:
    # \s+       means 1 or more whitespace characters                                   
    # [0-9]{2}  means exactly 2 characters of 0 through 9                               
    # \.        means find a '.' character                                              
    # [0-9]{4}  means exactly 4 characters of 0 through 9                               
    newfilename = re.sub("\s+[0-9]{2}\.[0-9]{4}", '', filename)
    newfilename = newfilename.replace(" ","_")
    os.rename(filename, newfilename)

Side Note

# Remove whitespace from files where EPG named with space " " replace with "_"
for filename in os.listdir("."):
    if filename.find("2013|09 ") > 0:
        newfilename = filename.replace(" ","_")
        os.rename(filename, newfilename)

Unless I'm mistaken, the from the comment you made above, filename.find("2013|09 ") > 0 won't work.

Given the following:

In [76]: filename = "EPG CRO 24 Kitchen 09.2013.xsl"
In [77]: filename.find("2013|09 ")
Out[77]: -1

And your described comment, you might want something more like:

In [80]: if filename.startswith('EPG') and ' ' in filename:
   ....:     print('process this')
   ....:     
process this

Upvotes: 1

Vivek
Vivek

Reputation: 920

How about little slicing:

newfilename = input1[:input1.rfind(" ")].replace(" ","_")+input1[input1.rfind("."):]

Upvotes: 1

utdemir
utdemir

Reputation: 27216

If dates are always formatted same;

>>> s = "EPG CRO 24 Kitchen 09.2013.xsl"
>>> re.sub("\s+\d{2}\.\d{4}\..{3}$", "", s)
'EPG CRO 24 Kitchen'

Upvotes: 1

aayoubi
aayoubi

Reputation: 12069

If all file names have the same format: NAME_20XX_XX.xsl, then you can use python's list slicing instead of regex:

name.replace(' ','_')[:-12] + '.xsl'

Upvotes: 1

Related Questions