0% found this document useful (0 votes)
42 views

File Handling

Uploaded by

Saqlain Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

File Handling

Uploaded by

Saqlain Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

File Handling

File
• File is a named location on the system storage which records data for later
access. It enables persistent storage in a non-volatile memory i.e. Hard disk.

• In Python, file processing takes place in the following order.


• Open a file that returns a filehandle.
• Use the handle to perform read or write action.
• Close the filehandle.
• Before you do a read or write operation to a file in Python, you need to open
it first. And as the read/write transaction completes, you should close it to
free the resources tied with the file.
Open a file

• To read or write to a file, you need to open it first. To open a file in Python, use its
built open() function. This function returns a file object, i.e., a handle. You can use
it to read or modify the file.

• We use open () function in Python to open a file in read or write mode. As


explained above, open ( ) will return a file object. To return a file object we
use open() function along with two arguments, that accepts file name and the
mode, whether to read or write. So, the syntax being: open(filename, mode).
There are three kinds of mode, that Python provides and how files can be opened:
• “ r “, for reading.
• “ w “, for writing.
• “ a “, for appending.
• “ r+ “, for both reading and writing
• One must keep in mind that the mode argument is not mandatory. If
not passed, then Python will assume it to be “ r ” by default.
Reading a file
# 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.
• 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.text", "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

# Python code to illustrate read() mode character wise


file = open("file.txt", "r")
print (file.read(5))
Writing a file
• Write operation is used to write the file.
• Let’s see how to create a file and how write mode works:
To manipulate the file, write the following in your Python
environment:
# 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.
Append in a file

# Python code to illustrate append() mode


file = open('geek.txt','a')
file.write("This will add this line")
file.close()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy