In this blog, I am simply wanting to write some data to file so that my program can read it during a subsequent restart.
I’m running Python 3 under Raspbian on a Raspberry Pi computer, so I am only going to deal with that system. I shall ignore the complexities of input/output as far as possible.
Python holds its variables in many different formats such as text, integer, floating point and binary. I want to store some integer values from a list variable and then to read them back at some future time.
The first step is to tell the system the name of the file and the directory where I want to store it. The directory can be on any resource that I can access from the Pi, but in this case I will store it in the Pi’s Downloads directory. I will store the full path and file name (motorPosition.text) in a variable called logfile, using the statement:
logfile='/home/pi/Downloads/motorPosition.text'
Now I need to open the file with the parameter ‘w+’ which means it is opened for writing, as shown below. The file will be created if it does not already exist. If it does exist, any contents will be deleted when the file is opened. (If I want the new contents to be added, then I need to use the ‘a’ (append) parameter instead of w+. Note that the open command can fail for a variety of reasons, but as we aren’t trapping errors, this would make execution halt.
Next we create a string of text. MotorPos is a list of integer values, so the for loop takes each value from the list, converts it to a string and appends it to the string called text. Each value is followed by a newline character (‘\n’). This will mean that each value will be on a separate line in the file.
When all the values in the list have been added to the string, the for loop terminates and the next statement writes the text to the file.
The next line closes the file. This is important as it ensures that the output buffer is flushed and the system resources are released.
file = open(logfile,'w+') # store motorPos on SD card text='' for line in motorPos: text = text+str(line) +'\n' #append values to string file.write(text) # write string to file file.close() # flush buffer and release resources
Next we are going to try to read the values back into the list, using a try structure to trap a FileNotFound error. If the file is present, it is opened for reading and a for loop reads lines 0 to 6 into motorPos, converting each string value to an integer. It prints the result and then closes the file. (For reasons best known to its authors, range loops stop when the parameter reaches the upper limit rather than when it exceeds the upper limit.)
However, if the file does not exist, the program takes the necessary action (in this case, it is simply to pass control to the next statement). Without the try structure, execution would terminate if the file is not present.
try: # Use motorPos positions from file if present
file = open(logfile,'r+')
for line in range(0,7):
input_text = file.readline()
motorPos[line] = int(input_text)
print (motorPos)
file.close()
except FileNotFoundError: # File not present
pass
Input/output is error-prone on any computer system and it is important to trap errors, otherwise programs will often fail for reasons that are unfathomable to the ordinary user.
I’m sure that Python aficionados will point out more elegant ways of doing this, but this is the most obvious way that I could fathom. The documentation is quite fragmented, so that many sophisticated features are quite obscure.
Editing the file
You can read the file using the Pi’s built-in text editor. It will look something like this:
8 516 404 472 592 1824 180
You can then edit the file, which could be used to supply starting parameters to the program. However, you will only be able to save the updated file if you have the necessary permissions, generally if you are the owner. I have found that if I let the program create the file, then I can view and copy the file but cannot save an update. However, if I create the file first using the text editor, then the program can write to it and I retain permissions to update it. This is just one of the complexities of input/output that i mentioned previously. Of course, file permissions can be changed by the superuser, but that is another ball game.











