How to write a data file to Beaglebone from Python

Now after all that I’ve decided not to write anything to the eMMC, fearing that I’ll cause some hiccup with the writes.
John
johnbakeree.blogspot.com

Now after all that I’ve decided not to write anything to the eMMC, fearing that I’ll cause some hiccup with the writes.

John

Eh ? What do you mean ? You do realize that you can create a tmpfs directory, that is located in memory, where you could then create files that are also placed in memory ?

William,
No, I did not know about writing to the tmpfs directory. That’s a great idea. I don’t know how to do it so will search for info on it.

I decided that I don’t need to write these particular files but can simply save the data to RAM. However, I will need to write a log file to a USB memory stick, so the tmpfs directory should fit the bill exactly to temporarily save the data before writing to USB.

Thanks very much for the suggestion.
John
johnbakeree.blogspot.com

http://www.cyberciti.biz/tips/what-is-devshm-and-its-practical-usage.html

One caveat however. Do not use /dev/shm. /dev/shm is meant to be used for POSIX IPC shared memory. So it’d probably work just fine, but using tmpfs is much better, and the directory used can be placed anywhere in a file system( within reason ).

So, at the command line: mount -t tmpfs -o size=512m tmpfs /mnt/ramdisk

Or, in /etc/fstab: tmpfs /mnt/ramdisk tmpfs nodev,nosuid,noexec,nodiratime,size=1024M 0 0

Also keep in mind, again, that the file / directory can be placed any where in the systems directory structure. So if you wanted a file somewhere in your home directory called “somefile” . . .

$ mkdir ~/test
$ mount -t tmpfs -o size=256m tmpfs /home/john/test

$ touch ~/test/somefile

One additional note I would like to leave on. I’ve used as much as 256m for a “ramdisk” on the beaglebone, so that does work. You can perhaps make it slightly bigger, but I would not push it. Typically though for a large single application, I’ve found that the beagelbone will use only up to around 120m. it really depends on what the beaglebone is doing.

And actually mount will need root permissions, so chown will need to be run on the tmpfs directory if one needs user rights to the directory.

I’m not a Python developer either, 'though I know it is very popular of late. Does it say something about Python itself that it did not throw up a big error in your face when the quotes were omitted? Or is the problem elsewhere?

The Python on my machine throws up an error.
$ python testopen.py
Traceback (most recent call last):
File “testopen.py”, line 1, in f= open(“T3.txt”, w) NameError: name ‘w’ is not defined

modeString = “w”
fileNameString = “out.txt”
f = open(fileNameString, modeString)
f.write(“This is a test for text output.”)
f.close()

Yes indeed. Python 2.7 in the Debian 3.8.13 bone50 on my BBB does flag the error, telling me that w is not correct.

I’m not sure as I haven’t gone back to my old code to try it again, but I suspect my use of Try/Except sort of hid the error, just telling me that the Try failed and not telling me exactly what the error was and I simply assumed the error was in the name or path in the open command. Clearly I should have just used the open command outside of the Try/Except and then an error message would have popped up telling me that the w was incorrect. Hopefully I have learned from this, first to double-check the syntax and second to dig a little to find out why the open command (or other command) didn’t work. I continue to be embarrassed at this dumb mistake but I am going on to probably make other mistakes. However, my code is working perfectly, GUI and all, including writing a logfile to the eMMC.
Thanks for everyone’s help. I certainly needed it.
John

Yes indeed. Python 2.7 in the Debian 3.8.13 bone50 on my BBB does flag the error, telling me that w is not correct.

I’m not sure as I haven’t gone back to my old code to try it again, but I suspect my use of Try/Except sort of hid the error, just telling me that the Try failed and not telling me exactly what the error was and I simply assumed the error was in the name or path in the open command. Clearly I should have just used the open command outside of the Try/Except and then an error message would have popped up telling me that the w was incorrect. Hopefully I have learned from this, first to double-check the syntax and second to dig a little to find out why the open command (or other command) didn’t work. I continue to be embarrassed at this dumb mistake but I am going on to probably make other mistakes. However, my code is working perfectly, GUI and all, including writing a logfile to the eMMC.

Thanks for everyone’s help. I certainly needed it.

Usually, with try / catch blocks, you’re given and error object back, from which you can print out the error.message text / character object. So unless you had something like . .

try{
. . .

} catch(e){

print(e.message);

}

You were using the try catch block incorrectly :wink:

On Thu, 14 Apr 2016 16:40:39 -0700 (PDT), John Baker
<bakerengineeringco@gmail.com> declaimed the
following:

should have just used the open command outside of the Try/Except and then
an error message would have popped up telling me that the *w *was
incorrect. Hopefully I have learned from this, first to double-check the

  The first thing to learn is: never use a bare "except" for production
code. If you are going to wrap something in "try/except" you should have
some idea of which exceptions might be raised and therefore should be named
in the "except" statement. That way, you intercept only the ones you expect
might be seen and for which you are prepared to do corrective action.

  If the only purpose of the "except" is to print some simplified
message, a la "something failed", then you are better off to not use a
"try/except" block at all, and let the exception kill the program and dump
a traceback that would tell you what failed.

Thanks All for the help and suggestions on Try/Catch and other. More evidence of the especially helpful folk on the Beagleboard users forum.
John

Thanks All for the help and suggestions on Try/Catch and other. More evidence of the especially helpful folk on the Beagleboard users forum.
John

No problem John, and hopefully we weren’t too obvious. The key point one should take away from this whole discussion though is: Whenever in code something could fail, it needs to be tested for. Then if you’re debugging, you should at minimum implement a method to print out that something failed, and why. In C, this is often referred to as printf() debugging, and actually can be used to trouble shoot most program errors.

In the case where you’re forced to use try / catch blocks however. Remove these try / catch blocks when done debugging IF you can. Some cases, in some languages, you’re all but forced to use try / catch blocks for things such as checking if a network connection exists. Anything really, that your application may need, but may not necessarily be active at any given point in time - While your application is running.