Since I’m using a text document instead of a database for simplicity, we first need to understand how to open, read, and write files in Python before diving into anything else.
ദ്ദി ʕ-ᴥ• ʔ
The first step is understanding the different file modes and how they determine where data is positioned within the file. But don’t worry, Python’s built-in open function already simplifies this process, making it much easier than what’s outlined in the BSD library function manual.
‘’r’’: Opening the file for reading (starts at the beginning)
‘’r+’’: Opens the file for reading and writing (starts a the beginning)
‘’w’’: Opens the file for writing (It clears the pre existing content starting from the beginning or creates a new file)
‘’w+’’: Opens the file for reading and for writing (It clears the pre existing content starting from the beginning or creates a new file)
‘’a’’: Opens the file for writing (Creates new file if it does exist, writes and the end)
‘’a+’’: Opens the file for reading and writing (Creates a file if it doesn’t exist, reads normally but writes a the end)
Okay since that is out of the way, we can start coding ʕ ᵔᴥᵔʔ
Lets try understanding the syntax that is usually used within this kind of coding. The usual syntax is ‘file = open(“filename.txt”, “mode”)’.
- ‘File’ : being the variable (if needed)
- ‘Filename.txt’ :the name of the file (either being called or being created)
- ‘Mode’ : the mode that you want to open the file
Reading
For reading, the code would look something like this:
- ‘With open()’: this automatically closes the file after the user. It is an easier way then added an extra line of ‘file close’
- ‘File.read’ – reads the whole file. Within the code, we have placed this into a variable called ‘content’ so we can print it out to the user
- ‘print(content)’: will output the file contents out to the user
In simple terms, this file reads the selected document and outputs its contents to the user in the console. While there are many ways to achieve this, I find this method to be the simplest.
We can also modify the code to read the file line by line using ‘print(f.readline())’, or even specify how many characters to read with print ‘(f.read(5))’. There are many ways to evaluate and refine your code.
Writing
With writing in your code, it is quite similar to reading. It uses the same syntax but uses ‘file.write’ instead of ‘file.read’
- ‘File.write’ : writes within the file. It has the same syntax as a print function
- ‘/n’: New line
Again, just like with reading, you can add more code to customise it exactly how you want. For our project, we’ll be taking input from the user and saving it to a text file, but we’ll get to that soon. Let’s keep it simple!
Checking if Files exist
Since we can create files using this command, we can also check if they exist! This process is a bit different from what we’ve done so far. First, we need to import the os module and then create an if statement. It would look something like this:
if os.path.isfile(filepath): print(“File exists”)
- ‘Import os’ : This gives us the ability to create, remove and change directories as well as manipulate files. In this instance, we are checking if the file exists within the folder.
- ‘If’: This would be the if statement that we use. If the text file is there within the directory, we will print out to the console ‘file exists!’. ‘Else’, we will print out ‘doesn’t exist!’
- ‘Os.path.isfile’ : checks if a given path is a file.
Of course there are also different ways of even checking files, which maybe I’ll look at later.
I’m gonna be honest, this post is getting long so i’ll make a part 2 on how we will be implementing this within the final code! let’s code ‘em all! ʕ -ᴥ•ʔ♡
Resources:
- https://www.manpagez.com/man/3/fopen/ (Understanding what the different modes within the open function mean)
- https://www.w3schools.com/python/python_ref_file.asp (methods avaible for file objects)
- https://www.geeksforgeeks.org/os-module-python-examples/ (os modules in python)
Leave a comment