Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 15th, 2003, 09:01 AM
eddiembabaali eddiembabaali is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Posts: 10 eddiembabaali User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
file renaming

I have files exported from power point presentation in the format .
Slide1.jpg
Slide2.jpg
Slide3.jpg
Slide4.jpg
Slide5.jpg
Slide6.jpg
Slide7.jpg
I need to rename like

Slide1.jpg => so1.jpg
Slide2.jpg => so2.jpg
Slide3.jpg => so3.jpg
Slide4.jpg => so4.jpg
Slide5.jpg => so5.jpg
Slide6.jpg => so6.jpg
Slide7.jpg => so7.jpg

Then rename the new files using this pattern

s01.jpg =>s00L.jpg
s02.jpg =>s00R.jpg
s03.jpg =>s01L.jpg
s04.jpg =>s01R.jpg
s05.jpg =>s02L.jpg
s06.jpg =>s02R.jpg
s07.jpg =>s03L.jpg
s08.jpg =>s03R.jpg

Any pointers/scripts will be highly appreciated. This is all on windows 2000

Reply With Quote
  #2  
Old October 15th, 2003, 09:43 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Take a look at the rename() and renames() function in the os module (http://www.python.org/doc/current/lib/module-os.html)

So if i've got this write you want to rename Slidex.jpg to s0x.jpg then rename that to s00x.jpg. why not just rename Slidex.jpg to s00x.jpg? This would make allot more sence, at least to me.

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old October 15th, 2003, 09:53 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
If you ment that you wanted to make a copy of s0x.jpg and the rename that to s0xx.jpg, you might find the shutil module interesting (http://www.python.org/doc/current/l...ule-shutil.html).

You should also be able to find a few nice little scripts in the Python Cookbook on ASPN (http://aspn.activestate.com/ASPN/Python/Cookbook/)

If you need any help with this i'd be happy to lend a hand , i'n any case it'd be interesting to seeing what you come up with..

Mark.

Reply With Quote
  #4  
Old October 15th, 2003, 10:22 AM
irishtek irishtek is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Tucson AZ
Posts: 29 irishtek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to irishtek Send a message via AIM to irishtek Send a message via Yahoo to irishtek
I would use os.listDir to get all filenames in the directory.

Then I would parse the list keeping only those that have the extension .jpg (this of course assumes that you only have the jpg files you want to rename in the directory that listDir parsed)

Call this new list ExistingFiles
And copy it to a list called RenameFiles1

Loop over RenameFiles1 and replace 'Slide' with 'so'
Also copy it to a list called RenameFiles2

The second Rename is probably the trick your looking for...
You would Loop over RenameFiles2 but you would want to loop with an increment of 2. So your counter would be 1,3,5,7,9, etc...
NOTE: This loop is renaming two filenames in each loop. which is why you want to increment by 2. and your loop counter will be 'odd numbers'.

There are two parts to this second renaming process.
First is the number, second is the letter that follows. (L or R)

The number is equivalent to the counter divided by 2.
1/2 = 0
3/2 = 1
5/2 = 2
etc...

The letter is done in two parts and is why you would increment your loop by 2.
You would add the letter L to the renaming of the filename at the counter position.
Then you would add the letter R to the renaming of the filename at the counter position plus 1.

Now you should have two lists with the filenames you want to create.

Now you loop over the ExistingFiles list and the RenameFiles1 list as well as the RenameFiles2 simultaneously running an os.rename(ExistingFiles ,RenameFiles1 ) and an
os.rename(ExistingFiles ,RenameFiles2 ) simultaneously.

Don't forget to clean up the directory if you plan to reuse this - otherwise it'll be renaming a lot of files you don't want....and you'll have a mess. This will also be important when testing your program as you design it.

Last edited by irishtek : October 15th, 2003 at 10:31 AM.

Reply With Quote
  #5  
Old October 15th, 2003, 10:23 AM
eddiembabaali eddiembabaali is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Posts: 10 eddiembabaali User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Someone wrote me something in PHP but I am thinking of porting it to Python. Here is the php code anyway. My python is not that good by the way.


function inc_length ($num,$length) {
// $num is the number and $lenght is wanted length
if (strlen($num) >= $length) {
return $num;
} else {
return inc_length (0 . $num , $length);
}
}

$existing_filenames = glob('*.jpg');
$new_filenames = array();
foreach ($existing_filenames as $name) {
$int_value = (int)substr($name, 5, strrpos($name, '.') - 5);

$new_filenames[$name] = 's' .
inc_length((int)(--$int_value / 2), 2) .
((--$int_value % 2 == 0) ? 'R' : 'L') . '.jpg';
}

print_r($new_filenames);

Reply With Quote
  #6  
Old October 15th, 2003, 10:46 AM
eddiembabaali eddiembabaali is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Posts: 10 eddiembabaali User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks alot irishtek I clearly understand the process now. I will try to write something up in python. My python sucks alot and I have not been coding for a long time but I just love python.

Reply With Quote
  #7  
Old October 15th, 2003, 10:48 AM
irishtek irishtek is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Tucson AZ
Posts: 29 irishtek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to irishtek Send a message via AIM to irishtek Send a message via Yahoo to irishtek
not a problem - if you need help let me know I'll do what I can.

Reply With Quote
  #8  
Old October 15th, 2003, 10:57 AM
eddiembabaali eddiembabaali is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Posts: 10 eddiembabaali User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Irishtek spoil me and writesomething for me to start with. I have this massive codeblock. Especially the counter logic. spoil me and help me out. Just a dirty functioning script. I will modify it with time and add other features and keep you posted on the process.

Reply With Quote
  #9  
Old October 15th, 2003, 12:04 PM
irishtek irishtek is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Tucson AZ
Posts: 29 irishtek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to irishtek Send a message via AIM to irishtek Send a message via Yahoo to irishtek
There are A HUGE NUMBER of things left out of this right now... but it's enough to get you started and make the changes you need...

This is only for the second loop that we diescussed and I've commented out the code. Which will be entirely in my next post so it will be clear from this post.

Keep in mind the files are renamed but some changes may need to be made for your formatting - I have left a lot for you to do, I simply don't have the time right now to right this out..
have fun with it...and good luck


I tried to comemnt my code out and use friendly variables so the logic can be followed easier.

Reply With Quote
  #10  
Old October 15th, 2003, 12:05 PM
irishtek irishtek is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Tucson AZ
Posts: 29 irishtek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to irishtek Send a message via AIM to irishtek Send a message via Yahoo to irishtek
import os

#-- I have put 15 emoticon images in their own directory.
#-- all images end in .gif
#-- I will leave it up to you to modifyt the ExistingFiles List with the
#-- Proper image names - and ensure nothing else in the list exists.
ExistingFiles = os.listdir("c:\program files\python\yahooemoticons")
NumberOfFiles = len(ExistingFiles)

for loopCounter in range(1,NumberOfFiles,2):
LookFor1st = str(loopCounter) + '.gif'
# I need to find the second file
second = loopCounter + 1
LookFor2nd = str(second) + '.gif'
# what the first file is replaced with
Replace1stWith = 'so1'+str(loopCounter)+'L.gif'
# what the second file is replaced with, YES this is with the LookFor1st
Replace2ndWith = 'so1'+str(loopCounter)+'R.gif'
# where the first file is at in the list
Where1stAt = ExistingFiles.index(LookFor1st)
# where the second file is at in the list
Where2ndAt = ExistingFiles.index(LookFor2nd)
#-- remove the first file from the list
ExistingFiles.remove(LookFor1st)
#-- insert the new 1st file into the list
ExistingFiles.insert(Where1stAt,Replace1stWith)
#-- remove the second file from the list
ExistingFiles.remove(LookFor2nd)
#-- insert the new 2nd file into the list
ExistingFiles.insert(Where2ndAt,Replace2ndWith)

#-- if there's an odd number of files
if loopCounter < NumberOfFiles:
last = loopCounter + 2;
LookForLast = str(last) + '.gif'
ReplaceLastWith = 'so1'+LookForLast
WhereLastAt = ExistingFiles.index(LookForLast)
ExistingFiles.remove(LookForLast)
ExistingFiles.insert(Where1stAt,ReplaceLastWith)

# Existing Files now contains the new list...
print ExistingFiles

Reply With Quote
  #11  
Old October 15th, 2003, 12:36 PM
eddiembabaali eddiembabaali is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Posts: 10 eddiembabaali User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks a million Irishtek. This will help in my endless preaching here that opensource technologies ROCK !!

I will start from there and see how it goes.

Reply With Quote
  #12  
Old October 15th, 2003, 08:16 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
Also, eddie, you might want to take a look at ReMv.

Reply With Quote
  #13  
Old October 16th, 2003, 07:30 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Sorry, i got very interested in this little probem last night before bed, it took me a lil while to get the whole idea but i've written have a pretty flexable program (attached)

The program follows these steps:

- get a list of all jpg files in the root directory
- if the files begin with the word 'Slide' then do the first rename

- loop through 2 for each jpg in the directory.
- create a string of oldname => newname
- split the string and as [oldname, newname]
- check if the file group exists.
- rename the files.. or print warning message.

Mark
Attached Files
File Type: py slides.py (752 Bytes, 639 views)

Reply With Quote
  #14  
Old October 16th, 2003, 09:44 AM
eddiembabaali eddiembabaali is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Posts: 10 eddiembabaali User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have some pseudo code here that may better explain what I need. This dude needs both renames anyway.

## Begin Pseudo code

oldprefix = Slide
firstprefix = s0
secondprefix = s0


//list contents of directory into list/array

//step through array and sort with numerical value ascending

//how?
//strip filenames of prefix(prefix=Slide) and ".ext" =.JPG



// sort through new list with numerical values

#first rename

// set counter1 to 0


// first rename process

//walk thru array and write out
# newfilename = firstprefix + counter1 + ."file_extension"
//increment counter1

//second rename
set counter2 = 0

//for all odd numbers in dir list
newfilename = secondprefix + counter2 + L.file_extension
//for all the even ones
newfilename = secondprefix + counter2 + R.file_extension
//increment counter


Because the new process is going to be like this :

Slide1.jpg => s00.jpg
Slide2.jpg => s01.jpg
Slide3.jpg => s02.jpg
Slide4.jpg => s03.jpg
Slide5.jpg => s04.jpg
Slide6.jpg => s05.jpg
Slide7.jpg => s06.jpg

Then rename the new files using this pattern

s00.jpg =>s00L.jpg
s01.jpg =>s00R.jpg
s02.jpg =>s01L.jpg
s03.jpg =>s01R.jpg
s04.jpg =>s02L.jpg
s05.jpg =>s02R.jpg
s06.jpg =>s03L.jpg
s07.jpg =>s03R.jpg

Last edited by eddiembabaali : October 16th, 2003 at 09:48 AM.

Reply With Quote
  #15  
Old October 16th, 2003, 10:10 AM
cvchen cvchen is offline
Hi, I'm Calvin
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: LosAngeles, SanDiego, Houston
Posts: 50 cvchen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 7 sec
Reputation Power: 10
just my opinion, but...

i think it'd be better if we just gave you places to look and you actually experimented with it...

are all your .jpg files going to be starting in the format

Slide1.jpg Slide2.jpg etc...?

if so, then you know that you want everything in the filename string except "Slide". you can get this by doing a string slice:

'Slide1.jpg'[5:] will return '1.jpg'
'Slide3394230.jpg'[5:] will 'return 3394230.jpg'
etc...

there are more examples in the tutorial that explains slices of lists, tuples, strings, any sequences datatype. the tutorial should have accompanied your windows Python installation, check the start menu Python folder for an html file, and open that... the link for the tutorial is in there.

to turn a string representation of an integer into an integer value, do something like this:

int('123') will return 123 (int value)

therefore, int('3394230.jpg'[:-4]) will give you the int value 3394230

as netytan has mentioned, the os module is what you want to take a look at. if you have python, which i just assume you do, you can take a look through the global module index and look at the functions you need to get the filenames, and to rename the files. OR, you could just open up IDLE, and type "help('os') and that'll spit out a lot of goodies for you to read

i think these are all the pieces you need.

if you need to learn how something works, i've found that experimenting with IDLE is the best way to go. IDLE is possibly one of the best features of Python installation for new Python users (and even people that know Python fairly well)... you just type in commands and it'll spit the output back at you, it's really great.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > file renaming

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap