mattius
Posts: 87
Joined: Thu Jun 14, 2012 9:55 am

Saving files to usb in python?

Wed Mar 05, 2014 10:09 pm

Ok ok problems again,

I have a usb drive in, and i can see it mounted automatically in /devices/aabbcc/

if i check if that path exists in my script it does, when i save files there, it doesnt give any errors but the files do not appear on the usb drive.

Interestingly when my program exits or finishes its loop i get the usb drive mounted popup appearing (can i suppress this? its ruining my full screen program) which makes me think python is unmounting it?

User avatar
DougieLawson
Posts: 42177
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Saving files to usb in python?

Wed Mar 05, 2014 10:44 pm

You can't write to a USB drive without mounting it.

/dev/sda or /dev/sda1 is the raw device file, writing to those will overwrite the partition table and/or file allocation table and will destroy ALL data that may already exist on your USB stick.

sudo mkdir /media/usb
sudo mount /dev/sda1 /media/usb
nano /media/usb/filename.here.txt
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

mattius
Posts: 87
Joined: Thu Jun 14, 2012 9:55 am

Re: Saving files to usb in python?

Wed Mar 05, 2014 10:54 pm

Is that even in raspian, where i can see the usb drive is mounted in /media/aabbcc/

in the OS i can write files to that location, in python i cannot

User avatar
DougieLawson
Posts: 42177
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Saving files to usb in python?

Wed Mar 05, 2014 11:01 pm

If the drive is mounted then it depends on the mount options (uid, gid and umask).
Have a look with the mount command.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

mattius
Posts: 87
Joined: Thu Jun 14, 2012 9:55 am

Re: Saving files to usb in python?

Wed Mar 05, 2014 11:10 pm

i dont understand, i have no control over the mount command as raspbian automounts usb drives.

Can i just mount it again?

User avatar
jojopi
Posts: 3628
Joined: Tue Oct 11, 2011 8:38 pm

Re: Saving files to usb in python?

Wed Mar 05, 2014 11:21 pm

mattius wrote:in the OS i can write files to that location, in python i cannot
That suggests a problem with your code.

(Unless you are running the Python as a different user than the GUI. But then you would expect an error, not a silent failure.)

User avatar
DougieLawson
Posts: 42177
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Saving files to usb in python?

Wed Mar 05, 2014 11:23 pm

mount # see what it's got now.
sudo umount /media/foobar # unmount the media
sudo mount -o uid=pi,gid=pi,umask=0022 /dev/sda1 /media/foobar # mount with alternative options.

There's probably a auto-mounter config somewhere that defines what defaults you get, but as I don't use a GUI and I don't use auto-mounting on my RPi then I don't know for sure.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

mattius
Posts: 87
Joined: Thu Jun 14, 2012 9:55 am

Re: Saving files to usb in python?

Wed Mar 05, 2014 11:57 pm

ok,

so manually mounting it made no difference, the drive seems to unmount when python runs

when i try and write to it, it does not exist.

Return to “Python”