St.
Xavier’s College
of Management & Technology
NAAC Accredited with B++ Grade (1st Cycle)
Digha – Aashiyana Road, Patna-800011, Bihar
subject – python
Topic – file handling in python
Name – Ajay Singh
Roll no – BCA2024030
Department – computer science
Under the guidance of: Dr. Supriya Shree
1
Table of contents
• Introduction to file handling
• File handling operation
• Opening a file
• Various modes of file
• Types of file in python
• Advantages and disadvantages
• References
2
Introduction to file handling
• Python supports file handling and allows users to
handle files
• File handling in Python involves interacting with files on your
computer to read data from them or write data to them.
• Python provides several built-in functions and methods for
creating, opening, reading, writing, and closing files.
3
File handling operations
• Python provides a variety of file handling operations that
allow you to manage files effectively. Such as
• Opening a file
• Reading to a file
• Writing to a file
• Appending a file
• Closing a file
4
Opening a file
• To perform any file operation, the first step is to open the file.
• Python's built-in open() function is used to open files in
various modes, such as reading, writing, and appending.
• The syntax for opening a file in Python is –
• file = open(filename, mode)
5
Various modes of opening a file
• There are various file opening modes in python. such as
• ‘r’- open for reading
• ‘w’ – open for writing
• ‘a’ – open for writing , appending to the end of the file
Apart from these modes we also need to specify how the file must be handled :
• text (t): Used to handle text files.
• binary (b): used to handle binary files (images).
6
Codes & Output
7
Types of file in python
There are various types of files in python such as -
• Text files- it is understood as a sequence of characters
consist of alphabets, numbers, and other special symbols.
Files with extensions like .txt, .py, .csv etc are some examples
of text files .
• Binary files – it is stored in a computer in computer in a
sequence of bytes such as image , audio , video etc .
8
Advantages
• verstality: File handling in Python allows you to perform a wide range
operations
• 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.)
• 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 .
9
Disadvantages
• 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 .
• 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.
• Performance : File handling operations in Python can be slower than other
programming languages.
10
References
• https://www.geeksforgeeks.org (accessed on dated
28/11/2024)
• https://www.javatpoint.com (accessed on dated 28/11/2024)
• https://www.w3schools.com (accessed on dated 05/11/2024 )
11
ANY ?
QUESTI N
12
THANK
YOU !
13