Last updated: March 2026. Mastering file input and output (I/O) is essential for any developer or storage administrator. Whether you are parsing credential files, generating log reports, or automating storage arrays, Python provides robust built-in functions for file handling. According to recent Python developer surveys, efficient file system automation and clean EOL (End of Line) handling remain core competencies for backend scripting in 2026.

This is also called as file handling in python script. File handling in python deals with all kinds of operations such as open, read, write, and modify files. In the coming section, we will cover all file handling operations using a python script.
Being a Storage Administrator, file handling is very useful while storage automation. One of the most common examples is reading a credential file.
Modern Python scripts leverage context managers (the with statement) to handle files safely, ensuring that resources are automatically cleaned up even if exceptions occur. This eliminates common pitfalls like unclosed file handles and corrupted output during storage automation workflows.
In the coming section, we will see all kinds of operations that we can do with a file using python script.
Open Files In Python
Best Practices for Modern Python File Handling
When working with text files in Python, understanding character encodings (like UTF-8) and platform-specific line endings (` ` vs ` `) is critical. Python handles these transparently, but explicitly declaring your encoding prevents cross-platform bugs when moving scripts between Linux storage controllers and Windows administrative workstations.
The first thing we can do is to open a file to read. Let’s say you have a credential file, where you have stored the user name and password, and you want to read the file. Below is the sample code you can use to open a file in python script.
s=open(“cred.txt”,”r”)
In the above example, s is an object that we use to open a file cred.txt. Now, it does not do anything, it just opens the file. To read the content you need to use read() function. The below line of code reads the entire content of a file and store the data in variable file_content.
s=open(“cred.txt”,”r”)
file_content=s.read()
print(file_content)
Using Python Context Managers With Statement
While using open() and close() manually works, modern Python best practices strongly recommend the with statement. It automatically manages file closure as soon as the execution block exits, making your code cleaner and significantly less prone to resource leaks.
with open(“cred.txt”, “r”, encoding=”utf-8″) as s:
file_content = s.read()
print(file_content)
Now, reading the entire content of a file may not be useful. You may need to read the content of a file one by one. The below code will open a file and read the content line by line.
s=open(“cred.txt”,”r”)
file_content=s.readlines()
Now file_content is a list and to print the lines one by one we need to use a for loops.
for lines in file_content:
print(lines)
Write Data In Files Using Python
While doing storage automation, most of the time I use a python script to write the output of commands into a file. The process of writing data in a file is similar to reading files. The only difference is that you need to open the file in write mode.
The below line of code will open a file in write mode and write data to the file. You should always close the file using the close function. If you forget to close, then the data will not be stored in the file.
s=open(“file.txt”,”w”)
s.write(“First line of the file.”)
s.close()
Update Files Content Using Python
Handling Append Mode and File Updates Safely
As highlighted in modern Python coding standards, opening files in append mode ("a") is vital when you need to retain historical records while adding new entries. For deeper dives into file operations, consult resources like Real Python’s Guide on Reading and Writing Files and GeeksforGeeks Text File Handling.
Please note that write mode always overwrites the content of a file. Hence, to write additional data in the same, you need to open the file in append mode.
Below line of code will add a new line in an existing file.
s=open(“file.txt”,”a”)
s.write(“Second line of the file.”)
s.close()
Python File Handling Video Tutorial
File handling may be complex for a beginner. Hence, we have made a dedicated video tutorial on it. Watch this video for detail and live tutorials.
Subscribe to the YouTube channel as well and stay tuned for more videos in this video series.
Frequently Asked Questions About Python File I/O
Why is it important to close files or use with statements?
Leaving files open can cause memory leaks, locked files on the operating system level, and incomplete data writes due to un-flushed buffers. Using the with statement guarantees closure.
How does Python handle different operating system line endings?
Python normalizes newline characters internally. On Windows,
is automatically translated to
on write and back on read unless specified otherwise.