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

Neeraj Kumar (2201920100191) Python Ppt

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)
6 views

Neeraj Kumar (2201920100191) Python Ppt

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/ 12

GL BAJAJ INSTITUTE OF TECHNOLOGY AND

MANAGEMENT, GREATER NOIDA

FILE HANDLING OPERATIONS IN PYTHON

BY: NEERAJ KUMAR DASS (CSE-4)


ROLL NO: 2201920100191
INTRODUCTION
• File handling in Python is a powerful and versatile tool that can be used to perform
a wide range of operations. However, it is important to carefully consider the
advantages and disadvantages of file handling when writing Python programs, to
ensure that the code is secure, reliable, and performs well.
• 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.
FILE ACCESS MODES:
CREATING A FILE

• File handling can also be used for creating a file. Even files with different extensions
like .pdf, .txt, .jpeg can be created using file handling in Python. To create a file, the file
must be open for writing. To open a file for writing access mode of file must
be w, a, w+, a+. Access modes govern the type of operations possible in the
opened file. It refers to how the file will be used once it’s opened.
• Code:
OPENING A FILE

• Opening a file refers to getting the file ready either for reading or for writing. This
can be done using the open() function. This function returns a file object and takes
two arguments, one that accepts the file name and another that accepts the
mode(Access Mode)
• Syntax: f=open(filename, mode)
• Code:


CLOSING A FILE

• close() function closes the file and frees the memory space acquired by that file. It
is used at the time when the file is no longer needed or if it is to be opened in a
different file mode.
• Syntax: File_object.close()
• Code:
WRITING TO FILE
There are two ways to write in a file.
• write() : Inserts the string str1 in a single line in the text file.
Syntax: File_object.write(str1)
• writelines() : For a list of string elements, each string is inserted in the text file.
Used to insert multiple strings at a single time.
Syntax: File_object.writelines(L) for L = [str1, str2, str3]
Code: Output:
READING FROM A FILE
There are three ways to read data from a text file.
• read() : Returns the read bytes in form of a string. Reads n bytes, if no n specified,
reads the entire file.
Syntax: File_object.read([n])
• readline() : Reads a line of the file and returns in form of a string.For specified n, reads
at most n bytes. However, does not reads more than one line, even if n exceeds the
length of the line.
Syntax: File_object.readline([n])
• readlines() : Reads all the lines and return them as each line a string element in a list.
Syntax: File_object.readlines()
READING FROM A FILE
CODE: OUTPUT:
APPENDING TO A FILE
• When the file is opened in append mode, the handle is positioned at the end of the
file. The data being written will be inserted at the end, after the existing data. Let’s
see the below example to clarify the difference between write mode and append
mode.
Code: Output:
DELETING A FILE
Methods to delete a file using python:
1. Python Delete File using os. remove
2. Delete file in Python using the send2trash module
3. Python Delete File using os.rmdir

Code:
THANK YOU

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