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

Python_File_Handling1[1]

Uploaded by

rajat.tarun2002
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Python_File_Handling1[1]

Uploaded by

rajat.tarun2002
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

FILE HANDLING IN

PYTHON

SUBMITTED BY:
NAVEEN GUPTA
2201920100189
CSE-D
Python File Handling

• 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. Let’s start
with the reading and writing files.
Advantages of File
Handling
• Versatility: File handling in Python allows you to
perform a wide range of operations, such as creating,
reading, writing, appending, renaming, and deleting
files.
• Flexibility: File handling in Python is highly flexible, as it
allows you to work with different file types (e.g. text
files, binary files, CSV files, etc.), and to perform
different operations on files (e.g. read, write, append,
etc.).
• User–friendly: Python provides a user-friendly interface
for file handling, making it easy to create, read, and
manipulate files.
• Cross-platform: Python file-handling functions work
across different platforms (e.g. Windows, Mac, Linux),
allowing for seamless integration and compatibility.
Disadvantages of File
Handling
• Error-prone: File handling operations in Python can be
prone to errors, especially if the code is not carefully
written or if there are issues with the file system (e.g.
file permissions, file locks, etc.).
• Security risks: File handling in Python can also pose
security risks, especially if the program accepts user
input that can be used to access or modify sensitive
files on the system.
• Complexity: File handling in Python can be complex,
especially when working with more advanced file
formats or operations. Careful attention must be paid to
the code to ensure that files are handled properly and
securely.
• Performance: File handling operations in Python can
be slower than other programming languages,
File handling operations
in Python

• r: open an existing file for a read operation.


• w: open an existing file for a write operation. If the file
already contains some data then it will be overridden but
if the file is not present then it creates the file as well.
• a: open an existing file for append operation. It won’t
override existing data.
• r+: To read and write data into the file. The previous
data in the file will be overridden.
• w+: To write and read data. It will override existing data.
• a+: To append and read data from the file. It won’t
override existing data.
open() Function in
Python
• 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.

• Syntax:
• f= open(filename, mode)
example

• file = open(‘naveen.txt', 'r')


• for each in file:
• print (each)
output
Create file function in
python
• Just like reading a file in Python, there are
a number of ways to write in a file in
Python. Let us see how we can write the
content of a file using the write() function
in Python.
example
• print("NAVEEN GUPTA\n2201920100189\n")
• with open("naveen.txt", 'w') as f:
• f.write('Hello, world!\n')
• print("File naveen.txt created successfully.")
• print("THE FILE CONTAINS:-")
• file = open('naveen.txt', 'r')
• for each in file:
• print(each)
output
Write file function in python

• "w" – Write: this command will create a new text


file whether or not there is a file in the memory
with the new specified name. It does not return an
error if it finds an existing file with the same name
– instead it will overwrite the existing file.
• Syntax;
• f = open('filename.txt', 'w')
example

• print("NAVEEN GUPTA\n2201920100189\n")
• file=open('naveen.txt','w')
• file.write("This is the write command\n")
• file.write("It allows us to write in a particular file")
• file.close()
• print("File naveen.txt edited successfully.\n")
• print("THE FILE CONTAINS:-")
• file = open('naveen.txt', 'r')
• for each in file:
• print(each)
output
Read files function in
python
• There is more than one way to read a file in
Python. Let us see how we can read the content of
a file in read mode.
• Example 1: The open command will open the file
in the read mode and the for loop will print each
line present in the file.
• Syntax: f = open('filename.txt', 'r')
Example

• print("NAVEEN GUPTA\n2201920100189\n")
• print("THE FILE CONTAINS:-")
• with open("naveen.txt","r") as f1:
• print(f1.read())
• read()
output
Appending files function in
python
• rstrip(): This function strips each line of a file off spaces from
the right-hand side. lstrip(): This function strips each line of a
file off spaces from the left-hand side.
• It is designed to provide much cleaner syntax and exception
handling when you are working with code. That explains why
it’s good practice to use them with a statement where
applicable. This is helpful because using this method any
files opened will be closed automatically after one is done,
so auto-cleanup.

• Syntax:
• f = open('filename.txt', ‘w’)
• f.write(new text)
example
• print("NAVEEN GUPTA\n2201920100189\n")
• with open("naveen.txt", 'a') as f:
• f.write("\nthis is the new text")
• print("Text appended to file naveen.txt
successfully.")
• f.close()
• print("THE FILE CONTAINS:-")
• file = open('naveen.txt', 'r')
• for each in file:
• print(each)
output
Deleting files function in
python
• When any large program is created, usually there are small
files that we need to create to store some data that is needed
for the large programs. when our program is completed, so
we need to delete them. In this article, we will see how to
delete the files in Python.
• Methods to delete Python file
• Python Delete File using os.remove
• Delete files in Python using the send2trash module

• Syntax: os.remove(file name)


example
• import os
• print("NAVEEN GUPTA\n2201920100189\n")
• os.remove("naveen.txt")
• print("\nFile naveen.txt deleted successfully.")
output
THANKY
OU

You might also like