Command Palatte in Odoo 18.1 Spreadsheet - Odoo SlidesCeline George
How to Close Subscription in Odoo 18 - Odoo SlidesCeline George
Tips Management in Odoo 18 POS - Odoo SlidesCeline George
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptxRahul Jawarkar
What is FIle and explanation of text files.pptx
1. Files in Python
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.
TYPES OF FILES
Computers store every file as a collection of 0 s and 1 s i.e., in binary form.
Therefore, every file is basically just a series of bytes stored one after the other.
There are mainly two types of data files –
1. text file
2. binary file
A text file consists of human readable characters, which can be opened by any text
editor. On the other hand, binary files are made up of non-human readable
characters and symbols, which require specific programs to access its contents.
2. Text file
• A text file can be understood as a sequence of characters consisting of alphabets,
numbers and other special symbols.
• Files with extensions like .txt, json, html,xml,csv, etc. are some examples of text files.
• When we open a text file using a text editor (e.g., Notepad), we see several lines of
text.
Binary Files
• Binary files are also stored in terms of bytes (0s and 1s), but unlike text files, these
bytes do not represent the ASCII values of characters.
• Rather, they represent the actual content such as image, audio, video, compressed
versions of other files, executable files, etc.
• These files are not human readable. Thus, trying to open a binary file using a text
editor will show some garbage values.
• We need specific software to read or write the contents of a binary file.
3. OPENING AND CLOSING A TEXT FILE
• In real world applications, computer programs deal with data coming from
different sources like databases, CSV files, HTML, XML, JSON, etc.
• We broadly access files either to write or read data from it.
• But operations on files include creating and opening a file, writing data in
a file, traversing a file, reading data from a file and so on
4. r
Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the
default mode.
rb
Opens a file for reading only in binary format. The file pointer is placed at the beginning of
the file. This is the default mode.
r+
Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
rb+
Opens a file for both reading and writing in binary format. The file pointer placed at the
beginning of the file
5. w
Opens a file for writing only. Overwrites the file if the file exists. If the file
does not exist, creates a new file for writing.
wbOpens a file for writing only in binary format. Overwrites the file if the
file exists. If the file does not exist, creates a new file for writing.
w+
Opens a file for both writing and reading. Overwrites the existing file if the
file exists. If the file does not exist, creates a new file for reading and
writing.
wb+
Opens a file for both writing and reading in binary format. Overwrites the
existing file if the file exists. If the file does not exist, creates a new file for
reading and writing.
6. a
Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is
in the append mode. If the file does not exist, it creates a new file for writing.
ab
Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists.
That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
a+
Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists.
The file opens in the append mode. If the file does not exist, it creates a new file for reading and
writing.
ab+
Opens a file for both appending and reading in binary format. The file pointer is at the end of the file
if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for
reading and writing.
7. • # Opening a file in read mode
file = open("example.txt", "r")
• # Opening a file in write mode
file = open("example.txt", "w")
• # Opening a file in append mode
file = open("example.txt", "a")
• # Opening a file in binary read mode
file = open("example.txt", "rb")
8. Reading a File in Python
Reading a file in Python involves opening the file in a mode that allows for
reading, and then using various methods to extract the data from the file.
Python provides several methods to read data from a file −
read() − Reads the entire file.
readline() − Reads one line at a time.
readlines − Reads all lines into a list.
Example:
with open("avnt.txt", "r") as file:
content = file.read()
print(content)
10. Using readline() method
In here, we are using the readline() method to read one line at a time,
making it memory efficient for reading large files line by line
with open("avnt.txt", "r") as file:
line = file.readline()
while line:
print(line, end='')
line = file.readline()
11. Using readlines() method
Now, we are using the readlines() method to read the entire file and splits it
into a list where each element is a line
with open("avnt.txt", "r") as file:
lines = file.readlines()
for line in lines:
print(line, end='')
12. Writing to a File in Python
Writing to a file in Python involves opening the file in a mode that allows
writing, and then using various methods to add content to the file.
To write data to a file, use the write() or writelines() methods.
When opening a file in write mode ('w'), the file's existing content is erased.
file = open("avnt.txt", "w")
file.write("Hello, World!")
file.close()
13. Using the write() method
In this example, we are using the write() method to write the string passed
to it to the file.
If the file is opened in 'w' mode, it will overwrite any existing content.
If the file is opened in 'a' mode, it will append the string to the end of the
file −
with open("avnt.txt", "w") as file:
file.write("Hello, World!")
print ("Content added Successfully!!")
14. Using the writelines() method
In here, we are using the writelines() method to take a list of strings and
writes each string to the file.
It is useful for writing multiple lines at once −
lines = ["First linen", "Second linen", "Third linen"]
with open("avnt.txt", "w") as file:
file.writelines(lines)
print ("Content added Successfully!!")
15. Closing a File
Closing a file is essential to ensure that all resources used by the file are
properly released. file.close() method closes the file and ensures that any
changes made to the file are saved.
file = open("avnt.txt", "r")
# Perform file operations
file.close()
16. Handling Exceptions When Closing a File
It's important to handle exceptions to ensure that files are closed properly,
even if an error occurs during file operations.
try:
file = open(“avnt.txt", "r")
content = file.read()
print(content)
finally:
file.close()
17. Advantages of File Handling in Python
Versatility : File handling in Python allows us 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 us 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.
18. Disadvantages of File Handling in Python
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