Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
169 views

Files in Python

Files in Python can store data permanently on storage devices. There are two main types - text files which store strings/characters, and binary files which store data like images/audio as bytes. To perform read/write operations, a file must first be opened using open(), operations performed, then closed using close() to prevent corruption. Common functions to read/write include read() to retrieve all file contents, readline() for single lines, and write() to output text to a file.

Uploaded by

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

Files in Python

Files in Python can store data permanently on storage devices. There are two main types - text files which store strings/characters, and binary files which store data like images/audio as bytes. To perform read/write operations, a file must first be opened using open(), operations performed, then closed using close() to prevent corruption. Common functions to read/write include read() to retrieve all file contents, readline() for single lines, and write() to output text to a file.

Uploaded by

SOURABH RODAGI
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Files in Python

Definition of file and its advantages


A file is a logical collection of data stored as a single unit inside a
filesystem on a storage devices. Data is stored permanently. The content
of file can be accessed sequentially or randomly.
Advantages
1.Data is stored permanently hence it persist even after program
termination
2. New data can be added and unnecessary data can be deleted and
existing data can be modified
3. Once the data is stored in a file the same data can be shared by
various programs
4.We can store large amount of data
Types of files in python
Text file: Stores the data in the form of characters. For example
employee name “Ram” is stored as six character and salary 6000.00
stored as 7 characters. Text files are used to store strings and characters
Binary file: It stores data in the form of bytes for example a single
character one byte and for integer 8 bytes. When the data is retrieved
from binary file it is retrieved as bytes. Binary files are used to store
audio and video and images
Opening and closing of a file
To perform read and write operations on a file we must first create a file
and then we must open file to perform write operation and read operation.
Once the read and write operations are performed we must close the file.
open(): open() is used to open a file. The syntax of open() is as follows
filehandler=open(“filename”,”openmode”)
Filehandler is a file object represent the file being opened
filename represents the name of the file to be opened
Openmode represents the purpose of opening file. There are different file
opening modes
File opening mode for text file
File open mode Description
w To write data into file. If any data is already present in the file, it would be deleted
and the present data will be stored
r To read data from the file. The file pointer is positioned at the beginning of the file

a To append data to the file. Appending means adding at the end of the existing data.
The file pointer is placed at the end of the file. If the file does not exist, it will create a
new file for writing data
w+ To write and read data of a file. The previous data in the file will be deleted

r+ To read and write data into file. The previous data in the file will not be deleted. The
file pointer is placed at the beginning of the file
a+ To append and read data of a file. The file pointer will be at the end of the file if the
file exists. If the file does not exist, it creates a new file for reading and writing
x To open the file in exclusive creation mode. The file creation fails if the file already
exists
File opening mode for binary file
File open mode Description
wb To write data into file. If any data is already present in the file, it would be deleted
and the present data will be stored
rb To read data from the file. The file pointer is positioned at the beginning of the file

ab To append data to the file. Appending means adding at the end of the existing data.
The file pointer is placed at the end of the file. If the file does not exist, it will create a
new file for writing data
w+b To write and read data of a file. The previous data in the file will be deleted

r+b To read and write data into file. The previous data in the file will not be deleted. The
file pointer is placed at the beginning of the file
a+b To append and read data of a file. The file pointer will be at the end of the file if the
file exists. If the file does not exist, it creates a new file for reading and writing
xb To open the file in exclusive creation mode. The file creation fails if the file already
exists
Closing a File
A file which is opened should be closed using the close() method. Once
the file is opened but not closed then the data of the file may be
corrupted or deleted in some cases.
Syntax for closing a file
filehandler.close()
filehandler i.e file object is being deleted means it is removed from
memory.
read() and write() functions
Once the file is opened we can perform read operation on a file using read()
function.
Syntax
f=open(“mydata”,”r”)
str=f.read()
print(str)
f.close()
read() reads all lines from the file pointed by file object f and stores it in string
object str
Python program to read the content of a file mydata
write() function
To perform write operation on a file write() is used
Syntax:
s=open(“s.data”, “w”)
s.write(“hello”)
p=“how are you”
s.write(p)
s.close()
Python program to read a line of text from keyboard and write it to a
file inputted by the user.
readline(),readlines()
Reads a line from the text file assumes that newline character as
indicator for end of line
Python program to read each line from a file biodata and count number
of lines using readline() function

Python program to read each line from a file biodata and count number
of lines using readlines() function

Python program to read each line from a file biodata and count number
of lines by iterating over the file object
continued
Python program to accept string from user until user press @ symbol.
Write the inputted string to a file input by the user

Python program to count number of lines words and characters from a


given text file

Python program to read a lines from text file that starts with from and
print it on screen
continued
Python program that generates the factorial of all integers from 1 to 10
and stores them in a file called fact.txt

Python program to read the numbers stored in a file test.txt. Check the
number is even or odd. If even write it to a file even.txt otherwise write
it to a file odd.txt

Binary file
Python program to copy an image file into another file

You might also like