0

I'm trying to rename some files in a directory using Python. I've looked around the forums here, and because I'm a newbie, I can't adapt what I need from what is out there.

Say in a directory I have a group of files called

FILENAME_002_S_0295_MR_3_Plane_Localizer__br_raw_20110602125225754_7_S110472_I238620.jpg FILENAME_002_S_0295_MR_3_Plane_Localizer__br_raw_20110602125236347_8_S110472_I238620.jpg FILENAME_002_S_0295_MR_3_Plane_Localizer__br_raw_20110602125236894_5_S110472_I238621.jpg FILENAME_002_S_0295_MR_3_Plane_Localizer__br_raw_20110602125248691_6_S110472_I238621.jpg

and I want to remove "125225754", "125236347", "125236894" and "125248691" here so my resulting filename will be FILENAME_002_S_0295_MR_3_Plane_Localizer__br_raw_20110602_7_S110472_I238620.jpg FILENAME_002_S_0295_MR_3_Plane_Localizer__br_raw_20110602_8_S110472_I238620.jpg FILENAME_002_S_0295_MR_3_Plane_Localizer__br_raw_20110602_5_S110472_I238621.jpg FILENAME_002_S_0295_MR_3_Plane_Localizer__br_raw_20110602_6_S110472_I238621.jpg

I'm trying to use the os.path.split but it's not working properly.

I have also considered using string manipulations, but have not been successful with that either.

Any help would be greatly appreciated. Thanks.

2
  • If they are always at the same indices just slice them out Commented May 19, 2014 at 21:34
  • @wim Thanks the indices are all the same Commented May 19, 2014 at 23:21

3 Answers 3

3

os.path.split splits a path (/home/mattdmo/work/projects/python/2014/website/index.html) into its component directories and file name.

As @wim suggested, if the file names are all exactly the same length, you can use string slicing to split out whatever occurs between two indexes, then join them back together. So, in your example,

filename = "FILENAME_002_S_0295_MR_3_Plane_Localizer__br_raw_20110602125248691_6_S110472_I238621.jpg"
newname = filename[:57] + filename[66:]
print(newname)
# FILENAME_002_S_0295_MR_3_Plane_Localizer__br_raw_20110602_6_S110472_I238621.jpg

This takes the first 58 characters of the string (remember in Python string indexes are 0-based) and joins it to all characters after the 67 one.

Now that you can do this, just put all the filenames into a list and iterate over it to get your new filenames:

import os

filelist = os.listdir('.')  # get files in current directory
for filename in filelist:
    if ".jpg" in filename:  # only process pictures
        newname = filename[:57] + filename[66:]
        print(filename + " will be renamed as " + newname)
        os.rename(filename, newname)
Sign up to request clarification or add additional context in comments.

1 Comment

I would like to do it automatically. I do not want to count from which number of character and to which number of character.
2

Can we assume that the files are all the same name up to the date _20110602[difference here]?

If that's the case then it's actually fairly easy to do.

First you need the index of that difference. Starting from character 0 which is 'F' in this case, count right until you hit that first difference. You can programatically do this by this:

s1 = 'String1'
s2 = 'String2'
i = 0
while(i < len(s1) && i < len(s2)):
  if(s1[i] == s2[i]) i++
  else break

And i is now set to the first difference of s1 and s2 (or if there is none, their length).

From here you know that you want to strip everything from this index to the following _.

j = i
while(j < len(s1)):
  if(s1[j] != '_') j++
  else break
# j is the index of the _ character after i
p1 = s1[:i] # Everything up to i
p2 = s1[j:] # Everything after j
s1 = p1.concat(p2)  
# Do the same for s2, or even better, do this in a loop.

The only caveat here is that they have to be the same name up to this point for this to work. If they are the same length then this is still fairly easy, but you have to figure out yourself what the indices are rather than using the string difference method.

Comments

2

If you always have exact string: '20110602' in the file names stored in 'my_directory' folder:

import re #for regular expression 
from os import rename
from glob import glob 

for filename in glob('my_directory/*.jpg'):
  match = re.search('20110602', filename)
  if match:
    newname = re.sub(r'20110602[0-9]+_','20110602_', filename)
    rename(filename, newname)

A more general code to match any YYYYMMDD (or YYYYDDMM):

import re #for regular expression 
from os import rename
from glob import glob

for filename in glob('my_directory/*.jpg'):
  match = re.search(r'\d{4}\d{2}\d{2}\d+_', filename)
  if match:
    newname = re.sub(r'(\d{4}\d{2}\d{2})(\d+)(_)', '\\1'+'\\3', filename)
    rename(filename, newname)

'\\1': This is match.group(1) that refers to the first set of parentheses

'\\3': This is match.group(3) that refers to the third set of parentheses

\d or [0-9]: are the same. They match any digit

{number}: the number of times the previous token (in this case a digit) are repeated

+ : 1 or more of previous expression (in this case a digit)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.