|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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); |
|
#6
|
|||
|
|||
|
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.
|
|
#7
|
|||
|
|||
|
not a problem - if you need help let me know I'll do what I can.
|
|
#8
|
|||
|
|||
|
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.
|
|
#9
|
|||
|
|||
|
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. |
|
#10
|
|||
|
|||
|
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 |
|
#11
|
|||
|
|||
|
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. |
|
#13
|
||||
|
||||
|
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 |
|
#14
|
|||
|
|||
|
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. |
|
#15
|
|||
|
|||
|
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. |