0% found this document useful (0 votes)
100 views4 pages

File Handling in Python

Python allows users to handle files by reading, writing, and performing other operations on them. The open() function is used to open a file and specify the mode (e.g. read, write, append). Various modes determine if the file will be overridden or data will be appended. The read() and write() functions allow extracting or writing strings of characters to files. Files can also be opened using with to ensure they are properly closed after operations.

Uploaded by

vidya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
100 views4 pages

File Handling in Python

Python allows users to handle files by reading, writing, and performing other operations on them. The open() function is used to open a file and specify the mode (e.g. read, write, append). Various modes determine if the file will be overridden or data will be appended. The read() and write() functions allow extracting or writing strings of characters to files. Files can also be opened using with to ensure they are properly closed after operations.

Uploaded by

vidya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

File Handling in Python

Python too supports file handling and allows users to handle files i.e., to read
and write files, along with many other file handling options, to operate on files.
The concept of file handling has stretched over various other languages, but the
implementation is either complicated or lengthy, but like other concepts of
Python, this concept here is also easy and short. Python treats files differently
as text or binary and this is important. Each line of code includes a sequence of
characters and they form a text file. Each line of a file is terminated with a
special character, called the EOL or End of Line characters like comma {,} or
newline character. It ends the current line and tells the interpreter a new one
has begun. 
Working of open() function
Before performing any operation on the file like reading or writing, first, we have
to open that file. For this, we should use Python’s inbuilt function open() but at
the time of opening, we have to specify the mode, which represents the
purpose of the opening file.
f = open(filename, mode)
Where the following mode is supported:
1. r: open an existing file for a read operation.
2. w: open an existing file for a write operation. If the file already contains some
data then it will be overridden.
3. a:  open an existing file for append operation. It won’t override existing data.
4.  r+:  To read and write data into the file. The previous data in the file will be
overridden.
5. w+: To write and read data. It will override existing data.
6. a+: To append and read data from the file. It won’t override existing data.

# a file named "geek", will be opened with the reading mode.


file = open('geek.txt', 'r')
# This will print every line one by one in the file
for each in file:
    print (each)
The open command will open the file in the read mode and the for loop will print
each line present in the file.
Working of read() mode
There is more than one way to read a file in Python. If you need to extract a
string that contains all characters in the file then we can use file.read(). The full
code would work like this: 
# Python code to illustrate read() mode
file = open("file.txt", "r")
print (file.read())

Another way to read a file is to call a certain number of characters like in the
following code the interpreter will read the first five characters of stored data
and return it as a string: 

 Python3

# Python code to illustrate read() mode character wise

file = open("file.txt", "r")

print (file.read(5))

Creating a file using write() mode


# Python code to create a file
file = open('geek.txt','w')
file.write("This is the write command")
file.write("It allows us to write in a particular file")
file.close()

The close() command terminates all the resources in use and frees the system
of this particular program. 

Working of append() mode


Let us see how the append mode works: 

 Python3

# Python code to illustrate append() mode


file = open('geek.txt','a')

file.write("This will add this line")

file.close()

Example: 
 Python3

# Python code to illustrate with()

with open("file.txt") as file: 

    data = file.read()

# do something with data

Using write along with the with() function


We can also use the write function along with the  with() function: 

 Python3

# Python code to illustrate with() alongwith write()

with open("file.txt", "w") as f:

    f.write("Hello World!!!")

split() using file handling


We can also split lines using file handling in Python. This splits the variable
when space is encountered. You can also split using any characters as we
wish. Here is the code:

 Python3
# Python code to illustrate split() function

with open("file.text", "r") as file:

    data = file.readlines()

    for line in data:

        word = line.split()

        print (word)

There are also various other functions that help to manipulate the files and their
contents. One can explore various other functions in Python Docs.

You might also like