Ok lost on why my python script is screwing up my fille ops


5 posts
by arehexes » Sun Dec 23, 2012 3:00 am
So I'm trying to write a python script to sort and move files based on the format name, and it works up to the moving part. I found out that it is because python has issues with the file path.

Code: Select all
for dirFile in os.listdir(currentFolder):
        try:
            #pulls the format type out of the file
            fileFormat = dirFile[dirFile.index(".")+1:]
            #moves the file to it's new destination
            #shutil.move(currentFolder+slash+dirFile, currentFolder+slash+formatArray[fileFormat])
            print currentFolder+slash+formatArray[fileFormat]+slash+"Broken part"
        except ValueError:
            pass       
        except KeyError:
            logFile.write(timeStamp()+"Unknown file format "+fileFormat+" add to "+sourceFile+" to fix"+"\n")
        except shutil.Error:
            logFile.write(timeStamp()+"Can not move file "+dirFile+"\n")


Results:
Expected:/media/usbstick/music/Broken part
Obtained:/Broken partick/music

This is a problem cause I can't transfer files without them screwing up.
Posts: 19
Joined: Tue Sep 18, 2012 12:11 am
by MattHawkinsUK » Sun Dec 23, 2012 8:24 am
Try printing the contents of each variable separately. Also if you are passing "currentfolder" I can't see how it can be empty. That is basic string manipulation and Python can do that fine.
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)
User avatar
Posts: 491
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
by IanH2 » Sun Dec 23, 2012 9:28 am
It looks as though the entries in formatArray might have some odd characters in.

A couple of Python tips may help:

1) look at the docs for os.path.split(), os.path.join() and friends. They're usually the right way to manipulate file names.

2) if you don't understand some output, try

print repr( something )

repr() will show things in their Python language representation, which is very useful for seeing odd characters in strings.

Cheers
IH
-----
https://github.com/IanHarvey
Posts: 65
Joined: Tue Dec 18, 2012 10:17 am
by arehexes » Tue Dec 25, 2012 2:27 am
ill try that when i get back home thanks.
Posts: 19
Joined: Tue Sep 18, 2012 12:11 am
by arehexes » Mon Dec 31, 2012 2:23 am
Thanks IanH2 for the advice, there was a carrage return that was rouge
Posts: 19
Joined: Tue Sep 18, 2012 12:11 am