arehexes
Posts: 22
Joined: Tue Sep 18, 2012 12:11 am

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

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.

User avatar
MattHawkinsUK
Posts: 533
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
Contact: Website

Re: Ok lost on why my python script is screwing up my fille

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)

IanH2
Posts: 79
Joined: Tue Dec 18, 2012 10:17 am

Re: Ok lost on why my python script is screwing up my fille

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

arehexes
Posts: 22
Joined: Tue Sep 18, 2012 12:11 am

Re: Ok lost on why my python script is screwing up my fille

Tue Dec 25, 2012 2:27 am

ill try that when i get back home thanks.

arehexes
Posts: 22
Joined: Tue Sep 18, 2012 12:11 am

Re: Ok lost on why my python script is screwing up my fille

Mon Dec 31, 2012 2:23 am

Thanks IanH2 for the advice, there was a carrage return that was rouge

Return to “Python”

Who is online

Users browsing this forum: No registered users and 19 guests