|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
My first script to organize.
Hello!
This is my first script that I tried to do. I have some ebooks that I wanted to organize according to authors name. What I wanted to do was create a folder with authors name in it, then move that book into that folder. The script that I got works and not work at the same time. It starts creating directory and moving files into , but then it stops with file not found error. Could somebody take a look and offer me some advice at how to fix and improve it? I wanted to try to use re to extract the authors name but that proved to be complicated task. tia Random import os,string,shutil filelist=os.listdir('D:/book') for file in filelist: dash=string.find(file,' -') comma=string.find(file,',') if comma>0 and dash>0: name=file[:dash] if not os.path.isdir('D:/book/' + name): os.mkdir('D:/book/' + name) shutil.move('D:/book/'+file,'D:/book/'+name+'/') else: shutil.move('D:/book/'+file,'D:/book/'+name+'/') print 'All Done' |
|
#2
|
|||
|
|||
|
Sorry tabs in the script did not work here.
Random |
|
#3
|
||||
|
||||
|
Is this yous code? the problem with indenting other peoples stuff if you have to guess where the block starts and ends
. Just for future referance the best way to include code is to click the '#' button from the menu or suround your code with [ CODE ][ /CODE ] tags (with no spaces).Code:
import os,string,shutil
filelist=os.listdir('D:/book')
for file in filelist:
dash=string.find(file,' -')
comma=string.find(file,',')
if comma>0 and dash>0:
name=file[:dash]
if not os.path.isdir('D:/book/' + name):
os.mkdir('D:/book/' + name)
shutil.move('D:/book/'+file,'D:/book/'+name+'/')
else:
shutil.move('D:/book/'+file,'D:/book/'+name+'/')
print 'All Done'
Also I have a few questions, how many books do you have, how far does it get before it bails i.e half way though.. also how are you getting the authers name? I'm guessing you have it as part of the ebooks title? I have a few idea's if you can give me a few example filenames that would be great, also, do they all have a comma and a dash?Take care, Mark. Last edited by netytan : September 4th, 2003 at 06:16 AM. |
|
#4
|
|||
|
|||
|
Hi Mark!
Yes this is my very first code created without any help. I just copied it over from notepab, in this little box it looked fine but after sending it I noticed that all indents were gone. Now all the books should follow this pattern but some are not. Drake, David - The Sea Hag.zip Cook, Rick - Wizards bane.zip I have couple of hundred books and it bails out some quarter of the way. Some of the books don't follow this format, so they should not be touched (ex: (html) David Drake The Sea Hag.zip) So it should create the folder only if it has comma and dash. tia Random |
|
#5
|
||||
|
||||
|
That would be why then Random, the if statment (line 6) checks if a comma is found, if some entries don't have a comma in there name then the script stops and hop's over to the next file name.
It sounds like allot of your titles don't follow this format if it's only going a quater of the way though.. I'm assuming that there is no error message being rased by your program? I'd so love to rewrite this code for you but since it's your first script on your own i'll stick to giving you pointers , in any case remove the whole comma counting thing from your script and it should work fine (in theory), it's sounding like all yout titles have the author - title format? If so you may wish to write a program to remover the comma from all the titles just to ensure the same formatting and etc. up to you..Have fun, Mark. |
|
#6
|
|||
|
|||
|
Not all the filenames follow the same format as author name - book title? Thats why I wanted first to organize first just the ones who do. The ones that don't follow this format should be left alone and not be placed in any folder. The way that I wanted the folders to be named is "Last, First" so yes the comma must stay and be part of the name. Please rewrite it! I don't think that I would be able to do a better job.
Random |
|
#7
|
||||
|
||||
|
Hi, ok all done, this should do that you wanted
.. of course if you weren't bothered about the ',' if would be a lil nicer . A few changes..1, I'm not using the string modle simply because I like the built-in's more. As far as I know, anything you can now do everything the string module you can do using the built-in string type's methods.. but u need Python 2.2+2, I desided to use os.rename() here instead of the shutil.move, saves importing two modules when you can do it with one ![]() 3, Just basic tidy up so it uses less code and is more readable, also string formatting (%) generally looks cleaner than escaping strings constantly (at least to me). Code:
import os
for filename in os.listdir('c:/books/'):
if os.path.isfile('c:/books/' + filename) and ' - ' in filename and ',' in filename:
title = filename.split(' - ')
if not os.path.isdir('c:/books/' + title[0]):
os.mkdir('c:/books/' + title[0])
os.renames('c:/books/%s' % (filename), 'c:/books/%s/%s' % (title[0], filename))
print 'Finished'
Oh, if there's anything else you need don't hesitate to ask, always happy to help, when I have the time of course. Have fun ![]() Mark. Last edited by netytan : September 4th, 2003 at 05:50 PM. |
|
#8
|
|||
|
|||
|
Thanks!
Now this is something simple but why am I getting this error? C:\books>python listdir.py Traceback (most recent call last): File "listdir.py", line 8, in ? os.renames('c:/books/%s' % (filename), 'c:/books/%s/%s' % (title[0], filenam e)) File "C:\Python22\lib\os.py", line 196, in renames rename(old, new) OSError: [Errno 2] No such file or directory |
|
#9
|
||||
|
||||
|
Ah sorry, my D:/ drive so a DVD drive so I had to to test it with 'C:/books/'.. if you change 'C:/' to 'D:/' it should work
![]() Mark. |
|
#10
|
|||
|
|||
|
No the drives are not the problem. It does goes to work creates some
directories and puts the files inside but then just stops with that error and I just don't see why. Well it created 10 directories and stoped? Random |
|
#11
|
||||
|
||||
|
Ok, just spotted soemthing although i dont know if it's aprt of the problem.. in py script I have os.renames(), can you change that to os.rename().. Give that a try. It works fine for me. Your using Python 2.3 I assume? Have you made any changes to the program? If so can you post the new code
![]() Mark. |
|
#12
|
|||
|
|||
|
A question? When the script is executed is there an order like "alphabetically" that it goes thrue? Because it seems to me that after creating 10 directories it encounters a name that it does not understand and just stops with the error? All I wanted was that if it finds a name that does not follow the rule ignore and continue.
tia Random |
|
#13
|
||||
|
||||
just thinking, the error is more than likly being caused by one of your filenames, the if statment only checks for the two char's being present but it's about as strict as we can easily get without using regular expressions. This title for instance may cause an error similar to the one your getting "some, author - the great - book".. fortunatly this is easily fixed. I've changed the os.renames() to os.rename() and set the limit in the .split() to 1.Code:
import os
for filename in os.listdir('c:/books/'):
if os.path.isfile('c:/books/' + filename) and ' - ' in filename and ',' in filename:
title = filename.split(' - ', 1)
if not os.path.isdir('c:/books/' + title[0]):
os.mkdir('c:/books/' + title[0])
os.rename('c:/books/%s' % (filename), 'c:/books/%s/%s' % (title[0], filename))
print 'Finished'
Mark. |
|
#14
|
||||
|
||||
|
os.listdir() to the best of my knowlage is in alphabetical order
. I'n theory thats what should happen (skip any files that don't follow the rules) but think the changes to the program should solve the error, if not i'm stumped ![]() Sorry about this, Mark. |