Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    12

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    12
    Sorry tabs in the script did not work here.
    Random
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    12
    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
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    12
    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
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    12
    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
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    12
    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
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    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
  24. #13
  25. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  26. #14
  27. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    12
    Found the problem.
    I put the line "print filename" right after "title = filename.split(' - ', 1)"
    and it showed me the books name that was causing the problem.
    The problem was caused buy double space in front of dash. After removing the extra space the execution continued.

    Thanks
    Random
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo