In Python, file handling requires no extra library to process files. Both text files and binary files can be operated in python. Unlike other programming languages file handling in python is pretty easy and can be implemented with minimum lines of code.
One thing worth noting is that python treats binary and text files differently and this should be kept in mind while working. Also, each file ends with an EOF indicator to tell the interpreter that the file has ended.
In this tutorial we will cover these topics on file handling:
Character | Meaning |
‘r’ | open for reading (default) |
‘w’ | open for writing, truncating the file first |
‘x’ | create a new file and open it for writing |
‘a’ | open for writing, appending to the end of the file if it exists |
‘b’ | binary mode |
‘t’ | text mode (default) |
‘+’ | open a disk file for updating (reading and writing) |
‘U’ | universal newline mode (deprecated) |
Points worth to note:
With Python you can create a .txt files (PyBlog.txt) by using the code, we have demonstrated here how you can do this .
We will be using in-built function open() to open the file in read and write mode. This function returns a file object. Let’s go through a step by step example to make you understand better.
file_new = open('pyblog.txt', mode='w+')
for i in range(11):
file_new.write('This is Line Number ' + str(i) + '\n')
Alternatively:
You can also use Python print() function to print on the file.
for i in range(11):
print('This is Line Number ' + str(i), file=file_new)
file_new.close()
Output:
This is Line Number 0
This is Line Number 1
This is Line Number 2
This is Line Number 3
This is Line Number 4
This is Line Number 5
This is Line Number 6
This is Line Number 7
This is Line Number 8
This is Line Number 9
This is Line Number 10
To append a file in python you have open the file in ‘a’ mode, to be on a safe side you can also open the file in ‘a+’ mode so that it doesn’t throw an exception is the file does not exists.
file_new = open('pyblog.txt', mode='a+')
for i in range(11, 21):
print('This is Line Number ' + str(i), file=file_new)
Alternatively:
for i in range(11, 21):
file_new.write('This is Line Number ' + str(i) + '\n')
file_new.close()
Output:
This is Line Number 0
This is Line Number 1
This is Line Number 2
This is Line Number 3
This is Line Number 4
This is Line Number 5
This is Line Number 6
This is Line Number 7
This is Line Number 8
This is Line Number 9
This is Line Number 10 // the file has been appended
This is Line Number 11
This is Line Number 12
This is Line Number 13
This is Line Number 14
This is Line Number 15
This is Line Number 16
This is Line Number 17
This is Line Number 18
This is Line Number 19
This is Line Number 20
By default the open() function uses the ‘r’ or read mode. (Just open it to read it :D)
file_new = open('pyblog.txt')
if file_new.mode == 'r':
for i in file_new:
print(i, end='')
Alternatively:
Use the read() method to read the file.
if file_new.mode == 'r':
c = file_new.read()
for i in c:
print(i, end='')
file_new.close()
This is Line Number 0
This is Line Number 1
This is Line Number 2
This is Line Number 3
This is Line Number 4
This is Line Number 5
This is Line Number 6
This is Line Number 7
This is Line Number 8
This is Line Number 9
This is Line Number 10
This is Line Number 11
This is Line Number 12
This is Line Number 13
This is Line Number 14
This is Line Number 15
This is Line Number 16
This is Line Number 17
This is Line Number 18
This is Line Number 19
This is Line Number 20
In Python, the print() function is a fundamental tool for displaying output. While printing simple…
Python is a versatile programming language known for its simplicity and flexibility. When working on…
PDF (Portable Document Format) files are commonly used for sharing documents due to their consistent…
PDF (Portable Document Format) files are widely used for document exchange due to their consistent…
Python is a high-level programming language known for its simplicity and ease of use. However,…
Object-Oriented Programming (OOP), iterators, generators, and closures are powerful concepts in Python that can be…
This website uses cookies.