Reading from and writing to files in python
Like other programming, reading data and information from files created in Python programming and writing data and information to those files, appending data to an existing file is a basic file handling task operation. Sometimes you may need to read and write specific data from files in a Python program or control, store, or process specific data and information.

So let’s manage the basic file read and file write operations in Python programming.
Reading from a file in a Python program.
To read data information from a file previously created in any Python program, you have to follow the file reading operation in Python program.
python Open File – To open a file in read mode (‘r’) in a Python program, use the file open() function in file operations.
file_location = ‘file.txt’
with open(file_location, ‘r’) as file:
# here it allow you to Read file operations
Read Python File Data – To access the contents of the current file and data in a Python file handling program, you can use the built-in Python file reading function methods such as read() function, readline() function, or readlines() function.
read() function – The read() function reads the entire file into the current Python program as a string format.
readline() – readline() reads only a single row from the file into the current Python program.
readlines() – readlines() reads all the row information of a file into the current Python program in a list format, where each file element behaves as a row.
Example of use of readlines() in a Python program.
with open(file_path, ‘r’) as file:
lines = file.readlines()
for line in lines:
print(line.strip()) # strip() removes any trailing whitespace and newline characters
Close the open file – Although using with automatically closes the current file in a Python program, remember to use the with option to close the file when your file handling task is complete.
Writing to a Python file.
To write data or information to a file in a Python program, follow the steps given below.
Open a file in Python – To write data or information to a file, use the open() function with ‘w’ write mode to open a file. Remember here, if the file does not exist in the system, then first create this file. If the file already exists in the system, then all other information previously present in this file will be removed.
file_location = ‘sample.txt’
with open(file_location, ‘w’) as file:
# open sample.txt file for write operation
with open(file_location, ‘w’) as file:
file.write(“welcome to file handling\n”)
file.write(“you can perform file write operation”)
Close the open file – In Python file handling operations like reading data from a file, with statement automatically closes the open file after the task is completed.
Appending to file in file handling.
If you want to add new text information to an existing file without removing its current content information, then you should use ‘a’ file append mode instead of ‘w’ write mode while opening that file.
file_location = ‘sample.txt’
with open(file_location, ‘a’) as file: # open file for append mode operation
file.write(“let we test file appned to add text info \n”) # this text line at the bottom of previous text
file.write(“the append function add text or line at the bottom of previous text \n”)
Python File Error Handling.
File Handling in Python Programs Always manage file errors while working with files. Common file handling errors include displaying read file (FileNotFoundError), displaying file write and append permission denied (PermissionError), or displaying disk full (IOError) in file operations.
Using try-except block to manage file errors in Python file handling is the method to solve the problem. For operations on files in certain conditions where file I/O operations are performed.
try:
with open(file_location, ‘r’) as file:
# open file for Read or write file handling operations
except FileNotFoundError:
print(f”Error: The file ‘{file_location}’ you search does not exist in system”)
except PermissionError:
print(f”Error: system denied permission to open file ‘{file_location}’.”)
except IOError as e:
print(f”file disk error generates {e}”)
File Handling Conclusion.
Always properly close files with with statement after use to empty system file resources in file handling operations.
Use with statement (context manager) to clean up file close operations automatically.
Properly manage file exceptions to manage common file errors during multiple Python file handling operations.