0% found this document useful (0 votes)
11 views8 pages

Python File Handling Basics

Python provides file handling capabilities, allowing users to read, write, and manipulate files in text or binary format. Files must be opened in specific modes such as read, write, append, or combinations thereof before performing operations. Various methods like file.read() can be used to extract data from files, including reading specific numbers of characters.

Uploaded by

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

Python File Handling Basics

Python provides file handling capabilities, allowing users to read, write, and manipulate files in text or binary format. Files must be opened in specific modes such as read, write, append, or combinations thereof before performing operations. Various methods like file.read() can be used to extract data from files, including reading specific numbers of characters.

Uploaded by

NUSRAT HUSSAIN
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

File Handling

• Python 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.
• Python treats files differently as text or binary.
File Opening
• Before performing any operation on the file like reading or writing, first, we have to open that file.

Where the following mode is supported:


1. r: open an existing file for a read operation.
2. w: open an existing file for a write operation. If the file already contains some data then it will be overridden.
3. a: open an existing file for append operation. It won’t override existing data.
4. r+: To read and write data into the file. The previous data in the file will be overridden.
5. w+: To write and read data. It will override existing data.
6. a+: To append and read data from the file. It won’t override existing data.
File Opening
The open command will open the file in the read mode and the for loop will print each line present in
the file.
File Opening
If you need to extract a string that contains all characters in the file then we can use [Link]()
File Opening
To read a file for certain number of characters.
The interpreter will read the first five characters of stored data and return it as a
string:
File Creation
Appending File
Splitting File

You might also like