A helpful tool in many automated programs is the ability to open existing files edit them and save the results. In many cases raw data is exported by the data collection device into a text file. So how can you create or modify a text file with Python Programming??
In this instructable I will be sharing what I've learning so far about File manipulation (file I/O) by sharing a Python Programming example of how to create a new text file and how to add data to an existing text file using Python.
Step 1: Python Module
The pic above is the Python 3 code that I will step through in this programming example. Skip to the last step for the link to the entire text file of this module.
Scope of the Module:
The code above consists of 2 functions. The first function named listnotebooks() looks in the folder that you specify and will print the names of all the files with the extension type you specify. In this example I am looking in my machines C: drive \ instructables folder for files with the .txt extension (all plain text files.)
The second function in this module I named takenotes() This function prints the basic instructions for the user and then calls the listnotebooks function mentioned above. The takenotes function lists any text files in the folder we mentioned above. The user then enters the name of text file they want to add text to.
With the new_file_name command being used if the user does not enter a file name that matches an existing file name a new file will be created. If an existing file name is entered by the user that file is appended with new text but any existing text will not be changed.
The next step Lets look at this Python Program module in action...
Step 2: Running the Module
The 1st pic above shows the folder I am interacting with before we run this Python Module.
As we can see there is no text file in the C:\instructables folder at this point.
The second pic is the running module of the file_IO.py file. Note that after the instructions no file names are listed. (This is a good sign since there is no text file there, if one were displayed the location in the Python Script is not 'pointed' to the correct folder location. )
In the 3rd pic above we can see where I told the program to create a file named didthiswork. From there I typed the lineIt was the best of times...
If all of this worked as planned I should be able to re-open the instructables folder in my C: drive and there should be a magically appeared text file with a sentence already inside it!!!
To be Continued....
Step 3: Did This Work??
So didthiswork did this work?? YES!!!
(It is very satisfying pulling this off for the first time.) But lets not rest. The screen shots above show running the file_IO.py module once more.
This time the pre-existing file in our folder didthiswork is displayed as an existing text file that can be appended if desired. If so if we type this text file name again we should be able to add to this existing file using our new Python Programming skills. You also can see in the shots above for this second re-opening test i typed in It was the worst of times...
Going back to the test file didthiswork and opening it up we see that IT WORKED AGAIN!! Our new line was added to the didthiswork text file.
Step 4: Closing Thoughts
Well that's all folks... Short and sweet...
I have more Python learning-s to come but that's all for this snipp-it. I hope this is a useful Python Tutorial example of how to create and edit text files .
Expanding on this for the future I am working to add exemption allowances for unexpected entries to prevent the module from crashing. I am also trying to work out the bugs of being able to add mutli-lines of text to a file in a single opening.
For a text copy of this Python Module use this link to my google share-all folder. Python File IO text copy
Lets go make something______________