Rename files in Python

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 8
Reputation: Mclovin is an unknown quantity at this point 
Solved Threads: 0
Mclovin Mclovin is offline Offline
Newbie Poster

Rename files in Python

 
0
  #1
Jun 17th, 2009
Hi!

I'm about to rename a hole bunch of files in different folders and want to use Python to do it. The problem is that I want to keep parts of the file name from the old file and insert it into the new file in another location.

for example:

old file: abcXXabc

new file: XXdefghi

Where the XX is the part I want to keep; always numbers. XX is'nt always on the same location in the old file. Any suggestions on how to do this?

I'm a newb when it comes to Python but heard it was great to use for these types of problems and maybe i can become more skilld afterwards!

this is what I got so far..

directory = '.'
for file in os.listdir('directory'):
re.search('([0-9]+)', file).group(1)
if '&' in file :
os.rename(file, file.replace('abcabc', 'defghi'))

Greatly appreciate it! /MCl
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 793
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Rename files in Python

 
0
  #2
Jun 17th, 2009
  1.  
  2. >>> s="abc123abc"
  3. >>> t= "".join([x for x in s if x.isdigit()]+[x for x in s if not x.isdigit()])
  4. >>> print t
  5. 123abcabc
  6. >>>
This was the quickest I got in my mind.

Try not to use regular expression. They are quite slow.
Last edited by siddhant3s; Jun 17th, 2009 at 10:33 am.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 8
Reputation: Mclovin is an unknown quantity at this point 
Solved Threads: 0
Mclovin Mclovin is offline Offline
Newbie Poster

Re: Rename files in Python

 
0
  #3
Jun 17th, 2009
thanks! although, at another forum I i recieved this code (thanks E.T) but it doesnt handle file extensions.. does anyone got suggestions on how to solve that? (I got several file types that needs to be renamed..)

  1. import os
  2. import re
  3. pattern = re.compile(r"abc(\d+)abc")
  4. tree = os.walk("/path/to/ur/dir")
  5. targets = []
  6. for dirpath, dirnames, names in tree:
  7. for name in names:
  8. m = pattern.match(name)
  9. if m:
  10. newname = os.path.join(dirpath, "%sdfghij" % m.group1))
  11. os.rename(os.path.join(dirpath, name), newname)
Last edited by Tekmaven; Jun 18th, 2009 at 5:20 pm. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Rename files in Python

 
0
  #4
Jun 17th, 2009
If you just want to replace certain extensions, you can use something like this:
  1. file_list = [
  2. 'list_nonunique1.py',
  3. 'Gene_read1.pyw',
  4. 'wx_psutil1.pyw',
  5. 'pa16names.txt',
  6. 'wxFrame_centerthings1.pyw',
  7. 'averagewords1.py',
  8. 'oneline.txt',
  9. 'tk_coinflip.pyw']
  10.  
  11. # replace extension .pyw with .py
  12. new_list= []
  13. for fname in file_list:
  14. if '.pyw' in fname:
  15. fname = fname[:-3] + 'py'
  16. new_list.append(fname)
  17. print(fname)
  18.  
  19. """
  20. my result -->
  21. list_nonunique1.py
  22. Gene_read1.py
  23. wx_psutil1.py
  24. pa16names.txt
  25. wxFrame_centerthings1.py
  26. averagewords1.py
  27. oneline.txt
  28. tk_coinflip.py
  29. """
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,024
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 288
woooee woooee is online now Online
Veteran Poster

Re: Rename files in Python

 
0
  #5
Jun 17th, 2009
A third option, if you want to keep the extension.
  1. old_file_name = 'bc13abc2.txt'
  2.  
  3. ## This assumes there is only one "." in the file name
  4. substrs = old_file_name.split(".")
  5. print substrs
  6. ## then use siddhant3s' example to find the digits, or some way
  7. ## using isdigit() and add substrs[1] (the ending) to that name.
  8. digits_list = [x for x in old_file_name if x.isdigit()]
  9. print digits_list
  10.  
  11. ## which is the same as
  12. second_list = []
  13. for x in old_file_name:
  14. if x.isdigit()
  15. second_list.append(x)
  16.  
  17. ## you then have to .join into a string
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 8
Reputation: Mclovin is an unknown quantity at this point 
Solved Threads: 0
Mclovin Mclovin is offline Offline
Newbie Poster

Re: Rename files in Python

 
0
  #6
Jun 18th, 2009
Thank you woooee and Sneekula for all the help!

I still got some issues though. I dont want to specify the old file name since I got a great number of files with different file names and want to change the syntax on all of them. I was'nt very clear on what had to be done and aplgz for this..

for example:

old files:
1. abcde22ab.txt
2. abcde23ab.jpg

and also

3. fghij22fg.txt
4. fghij23fg.jpg

..and want the new files to look like this:

1. 22KLMNO.txt
2. 23KLMNO.jpg

and also

3. 22PQRST.txt
4. 23PQRST.jpg

That is, I want to keep the number in the file name and put in first in the new file name, I want to be able to specify which letters that should be in the new file (for fghik, replace with PQRST in another location of the string) and also to keep the extension of the original file. I would greatly appreciate any help and tips on how to solve this! /MCl
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC