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

File Handling

A file is a collection of bytes stored on a storage device like a hard disk. There are two main types of files: data files, which store application-specific data for later use, and text files and binary files, which store information differently. Text files use ASCII or Unicode characters while binary files use bytes. Files must be opened, read from and written to using file objects in various access modes like read, write, and append before being closed.
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 views

File Handling

A file is a collection of bytes stored on a storage device like a hard disk. There are two main types of files: data files, which store application-specific data for later use, and text files and binary files, which store information differently. Text files use ASCII or Unicode characters while binary files use bytes. Files must be opened, read from and written to using file objects in various access modes like read, write, and append before being closed.
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
You are on page 1/ 7

FILE HANDLING

File:
A file is a bunch of bytes stored on some storage device
like hard disk.

Data Files:
 Data files are the files that store data related to a
specific application for later use.
 Data files are of two types.
o Text file
o Binary file
Difference between text file and binary file.
Text File Binary File
A text file stores the Binary file stores the
information in the form of information in the form of
stream of ASCII or stream of bytes.
UNICODE
characters(provide unique
number to every character).
Text file takes time for read There is no translation occur
and write operations because in binary files. So binary files
translation from every are faster and easier for
character to ascii takes place. program read and write than
text files.
In text file, each line is ended In a binary file, there is no
with a special character EOL character.
known as EOL(end of line),
i.e. ‘\n’ or ‘\r\n’
Text files are of two types, Binary file doesn’t have any
1. Regular text file type.
2. Delimited text file.

Different types of text files.


 Text files are of following two types.
 Regular text file
 A text file stores information in the form of text.
 In text file, each line of text is terminated with
special character is known as EOL.
 These files have an extension .txt
 Delimited text file
In these text files, a special character is stored to
separate the values.

 TSV (Tab separated value) File.


 When a tab character is used to separate two
values called as Tab Separated Value file.
 These file has an extension .txt or .csv.
 Ex:
I->am->a->text
 CSV(Comma Separated Value)File.
 When a comma is used to separate values
called as CSV file.
 CSV file can also use a delimiter other than
comma.
Like tild(~),pipe(|),etc.
 These files have an extension .csv
 Ex: I,am,a,text or I~am~a~text, etc.
Opening and Closing Files
1. Opening File
 In order to work with file, you need to open it in specific
mode.
 After opening a file different manipulation operations can
take place.
 Reading data from file
 Writing data from file
 Appending data to file
 To open a file, use following syntax
<file_objectname>=open(<filename>)

Or
<file_objectname>=open(<filename>,<mode>)
 Ex:
File1=open(“text1.txt”,”r”)

Or

File1=open(“C:\\File\\text1.txt”)

Or
File1=open(r”C:\File\text1.txt”,”r”)
Where,
File1 is a file object.
‘Text1.txt’ is a file opens in read mode and attaches it to
file object ‘File1’.
2. Closing File
 After performing operation on file, closing file is important.
In python.
 File are automatically closed after end of program, but it is
good practice to close it after performing operation on it.
 Syntax:
File_object.close()

File Object
 File object or File handle is the reference to a file on disk.
 It opens and make it available for different number of task.
 It is used to read and write data to a file on disk.

File Access Mode


Here is a list of the different modes of opening a file −

Sr.No. Modes & Description

1 r
Opens a file for reading only. The file pointer is placed at the beginning of the
file. This is the default mode.

2
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.

3
r+
Opens a file for both reading and writing. The file pointer placed at the beginning
of the file.

4
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.

6
wb
Opens 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.

7
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.

8
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.

9
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.

10
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.

11
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.

12
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.

Working with files


 Reading from a file

There are three ways to read data from a text file.


read() :
Returns the read bytes in form of a string. Reads n bytes, if no
n specified, reads the entire file.

File_object.read([n])
readline() :
Reads a line of the file and returns in form of a string.For
specified n, reads at most n bytes. However, does not reads more
than one line, even if n exceeds the length of the line.

File_object.readline([n])
readlines() :
Reads all the lines and return them as each line a string
element in a list.

File_object.readlines()

 Writing to a file

There are two ways to write in a file.


write() :
Inserts the string str1 in a single line in the text file.
File_object.write(str1)

writelines() :
For a list of string elements, each string is inserted in the
text file.Used to insert multiple strings at a single time.

File_object.writelines(L)
( L = [str1, str2, str3] )

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy