Python Intermediate Files
Python Intermediate Files
This page covers the intermediate files management of Python programming.
For Python basic file management here.
It continues the previous basic tutorials in more details. Intermediate files encompass:
- writing content in a file
- “write()” function
- Text space management symbols
Writing content in a file
In Python, file management allows writing text inside files. As previously explained, there are two main types of writing content, append and write (“a” and “w” modes).
The difference is that “append” will add new text in addition to the existing content – meaning, the already input text does not disappear. With “write” on the other hand, even if there is content inside the file, Python erases everything first and then writes the new text.
Remember that it is important to open and close the file after using it.
Below is an example of creating a file.
# create mode x = open("file.txt", "x") x.close()
Note that if the file already exists, the program produces an error.
It is a common practice to combine file management with “try/except” statements.
Whether opening a file with the write (“w”) or append (“a”) mode, the program always checks if the file exists. In both cases, if the file is non-existent, Python creates one. In addition, in both scenarios even if the file exists, the program does not produce an error.
# Both statements create the file if non-existent x = open("file.txt", "w") x.close() x = open("file.txt", "a") x.close()
Write()
The function of “write()” processes new text into a file. It works with both modes, append and write.
Below is an example of the “write()” method with the append mode.
# Add text to the file x = open("file.txt", "a") x.write("Adding text") x.close() # Read the text from the file x = open("file.txt", "r") print(x.read()) x.close() # Output: Adding text
The following is an example of the function “write()” with the write mode.
# Rewriting the file x = open("file.txt", "w") x.write("Rewriting text") x.close() # Read the text from the file x = open("file.txt", "r") print(x.read()) x.close() # Output: Rewriting text
Text space management symbols
The text space management symbols, called escape characters, allow Python to modify string space or bypass special characters.
The escape characters are useful when writing text inside a file.
These are:
- \t (tab)
- \n (new line)
- \ (escape symbols)
The following is an example of how to use “\t” and “\n” characters.
text = "This is an example." print(text) # Output: This is an example. text = "This is \tan example." print(text) # Output: This is an example. text = "This is an \nexample." print(text) # Output: This is an # Output: example.
Note that writing more content in a file adds it at the end and not on a new line. This in turn, can create unorganised text.
Therefore, implementing escape characters can be crucial.
The “\” symbol bypasses Python’s instructions to disallow some characters (e.g. quotation).
text = "This is "an" example." print(text) # Output: SyntaxError: invalid syntax
text = "This is \"an\" example." print(text) # Output: This is "an" example.
Another option is to apply one of the two quotation marks when declaring the string, and the other inside the string as text.
text = "This is 'an' example." print(text) # Output: This is 'an' example.
text = 'This is "an" example.' print(text) # Output: This is "an" example.