How to write a data file to Beaglebone from Python

I want to store a little data on the eMMC from my Python code. My program is not getting errors when I try to write a very short data file but it doesn’t actually seem to write it as I cannot read it back. I am using
f = open(‘MyFile.txt’, w)

or
f = open(’/home/debian/Desktop/MyFile.txt’, w)

or
f = open(‘MyFile.txt’, r)

or
f = open(’/home/debian/Desktop/MyFile.txt’, r)

to open the file and am not getting any errors but Python is not finding the file and I cannot find the file with a search from WinSCP.

So how to create a data file and where does it go?

Thanks,
John

I’m not a python developer, but actually have done this many times in C. So I can tell you this with certainty. This is done no different than it is done on any Linux system.

The point here is that you should go out and find any good python tutorial, that covers writing to a file, and follow it.

That’s what I figured William. As far as I can tell, I am doing the open and write and read like I have with C and as described in https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files. However, I am not getting any error message when I do the open for writing and do the write, just that the file is not getting created and written. It should be so straightforward but it’s not working.
John

That’s what I figured William. As far as I can tell, I am doing the open and write and read like I have with C and as described in https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files. However, I am not getting any error message when I do the open for writing and do the write, just that the file is not getting created and written. It should be so straightforward but it’s not working.

Does this file already exist ? You should check the documentation for the file write() method and see what all is required for the method to work correctly. Also, as I said I’m no python developer, but the code shown there is really bad form. There is no error checking on the file object when attempting the open() call. In C this would be done by checking the value of f, but I’m not even sure this is possible in python.

So I’d attempt to help you trouble shoot this by writing code myself, here, and testing. But I’m very “allergic” to python. Or more correctly, I’m getting old, and set in my ways, and python just is not in my future plans. ever . . .

python

import os
os.getcwd()
‘home/pwolfson’

Thanks Paul.
getcwd() tells me that my cwd is /home/debian/Desktop when I use f = open(‘MyFile.txt’, w). I am using Try/Except and not getting an exception with the open() statement. Perhaps I can’t write a text file to the Desktop, but it seems like it should give me an error message.
John

I made a new folder /home/baker and then changed my write open() command to f = open(’/home/baker/MyFile.txt’, w) and the read open command to f = open(’/home/baker/MyFile.txt’, w) but still am not able to create and write to the MyFile.txt. Now the getcwd() command tells me that the cwd is /home/baker. Also not getting any error message even for writing or reading, just the exception to reading the file that it cannot find the file. No exception when I try to write to the file. I am running my Python program with sudo python myprogram.py. I tried changing the permissions to my /home/baker directory with “sudo chmod -R 777 /home/baker” but WinSCP still says the owner is root and still my Python program cannot write a text file to /home/baker.

So I’m at wits end (a short trip to be sure) and cannot figure out how to write the data file.
John

John, the only other thign I can think of which may be a possibility. Is that your eMMC might perhaps be in read only mode ? Which does not explain the no error message thing, but why don’t you try to write a simple C app to do the same and see what happens ?

Sorry I should have added this o my last post. In C you check the file descriptor “object” for -1, and if -1 you check errno, which should be a value indicating which error you’re getting.

Another thing you can check, even before trying something with C, is dmesg | tail, and see if any errors crop up there. I would not think so, but I’ve been surprised before . . .

Thanks William but although I have done some C programming and have read and written files in C, I don’t know how to get C running on the Beaglebone. Hopefully I haven’t painted myself in a corner but my Python program is running well otherwise. I am doing some Fuzzy Logic control for a paint flow project, have a GUI with an animated graph using matplotlib and Tkinter, am outputting a PWM signal and measuring the filtered PWM voltage back into the controller software with the BBB ADC. It’s all looking good until I decided to write a very short text file for the system gain, just a single number, but am stymied by it. I just now made my own text file using nano and see that the file is in my /home/baker folder but my Python program cannot find it, cannot read it.
It seems likely that I’m just doing something wrong, some simple thing and perhaps I simply cannot read and write from Python to my text file, but that seems crazy.
John

It seems likely that I’m just doing something wrong, some simple thing and perhaps I simply cannot read and write from Python to my text file, but that seems crazy.

The thought that you’re doing something wrong did cross my mind, but I usually try to give a person a benefit of the doubt, until their skill level makes its self plain.

Maybe if you show us what you have for a source file someone can figure what’s going on. I can actually read through Python quite well despite never having write a single line of python myself. The syntax is not too far off other language I’ve used in the past.

William,
I don’t know what the dmesg command should show but I don’t see anything exciting except for:
CAUTION: musb: Babble interrupt occurred

Otherwise there is something about a hub, usb, ethernet libphy: PHY … not found and IPV6: phy nnnnn not found on slave 1.

Here’s some of my code. Hopefully you’ll see something newbie-wrong that I’ve done. It’s been a while since I have done file I/O, so it’s likely that I have some syntax error(s).
idcdggda.png

Thanks for your help,
John

On Tue, 5 Apr 2016 13:43:47 -0700 (PDT), John Baker
<bakerengineeringco@gmail.com> declaimed the
following:

I want to store a little data on the eMMC from my Python code. My program
is not getting errors when I try to write a very short data file but it
doesn't actually seem to write it as I cannot read it back. I am using
f = open('MyFile.txt', w)

  First problem -- the mode is a string...

f = open("MyFile.txt", "w")

or
f = open('/home/debian/Desktop/MyFile.txt', w)

or
f = open('MyFile.txt', r)

or
f = open('/home/debian/Desktop/MyFile.txt', r)

to open the file and am not getting any errors but Python is not finding
the file and I cannot find the file with a search from WinSCP.

  Did you ever write anything to the file, and did you close the file?

  f.write("Some junk\n")
  f.close()

  The first open should create the file in whatever your current working
directory is. Without connecting my BBB I can't confirm if the second is a
valid path.

  And last -- how are you running it? SSH to a command line?

Here’s some of my code. Hopefully you’ll see something newbie-wrong that I’ve done. It’s been a while since I have done file I/O, so it’s likely that I have some syntax error(s).

So, I would reduce the code you have, at least in a test file, and use only the absolute minimum code for printing a text string to a file. Then of course double check to make sure it works. I would also remove the Str conversion from the write() functional call, and make that an explicitly defined string before the call to write(). The “call” Str(some numerical value) also seems very odd to me. Usually with OOP languages you have something akin to value.toString(value to convert), but again this is where my lack of python BCL( base class library ) knowledge is going to leave me at a disadvantage.

You’re also not writing a new line character after your value into the file, which may or may not cause a problem.

Isn't the open syntax => open("filename.ext","r")?

If the directory /home/baker is showing as owned by root, chaos won’t change ownership of it. Try chown {user.group} /home/baker. Then, assuming you are running the application as {user} you should at least have write permissions to that directory, which you would not have of it was owned as root.

Jonathan

On Tue, 5 Apr 2016 19:11:32 -0700, John Baker
<bakerengineeringco@gmail.com> declaimed the
following:

Here's some of my code. Hopefully you'll see something newbie-wrong that
I've done. It's been a while since I have done file I/O, so it's likely
that I have some syntax error(s).

  To reiterate, the first thing is that ALL of your open() calls are
erroneous. The second argument to open() is a string, you seem to be
providing some object -- unless the objects r and w were earlier bound to
strings of "r" and "w", the open calls should be failing.

  Since you are using bare except clauses, you could be trapping on /any/
error and never identifying what the error is.

  You state you are not seeing any messages... What are the values of
"PaintFlow" and "control_output" at the start of that image (really? a
screen grab rather than text cut&paste?)? Could it be that you aren't even
entering that block because the if statement is coming up false?

  As often advised on comp.lang.python -- reduce your case to the minimal
that illustrates the problem. You think you are having problems creating a
file, so create a short test program that does nothing else.

Gosh, how did I miss that, that the mode is a string. Well it had to be something simple like that. Quoting the w and r fixed the problem. Very embarrassing.
:-[
Thanks,
John

Thanks All for your help. It is much appreciated and shows how helpful the BB Forum users are and how valuable the Forum is.
John