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

Introduction To File Handling

Uploaded by

naveen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Introduction To File Handling

Uploaded by

naveen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction to File Handling :

• Files are nothing but the collection of data.

• Files can be stored on a secondary memory device like hard disks, pen-drives or cds.

• Files are loaded in main memory(hard disk) or ram.

• Once the data is stored in computer file, you can retrieve it and use it as per the requirement

Advantage of storing data in file

 File is stored In computer hard disk permanently. Even though computer switched off ,the file
and it’s data is not removed.
 It is possible to update, modify the data whenever required.
 The stored data can be used by anybody whenever required
eg- A file containing student data can be used by department, office, staff
 File can be used to store huge amount of data
eg- voters list, data of all the students in a college

File Path Files are stored in secondary memory.


 Each file has a path which helps system to locate the file.
 In Windows path starts with drive name. (Ex c:\, d:\).
Example : C:\Users\Gamers>d:
D:\>cd python code
D:\python code>cd Unit 1
D:\python code\Unit 1>addition.py
 In Linux or allied operating systems, path starts from root (/) directory. (Ex. /home,/user).

Types of Files Python supports two types of files.


 The first one is a text file that store data in the form of text and readable by humans and
computers.
 The second one is binary file that store binary data and readable by computer only 1.

1.Text Contents (Text Files) :


 Here files have all data which is text.
 Text can be stored in various ways.
 Simplest files are .txt files Also famous document (.docx) is also text.

Some example formats are :


 Text Files (.txt)
 Document Files (.docx)
 Pdf Files (.pdf)
 Database Files (.csv)

2. Binary Contents (Binary Files) :


 Here files have binary data.
 Various image, video, and audio data can be stored here in binary form.
 Reading these files require special handling. Here data stored is processed byte by
byte. Binary
 files have various encodings. Encoding enables compressed or uncompressed storage.
 Additionally binary files include compressed files.
 Compressed files store data in minimal space.

Some example formats are :

 Image Files (.jpeg, .png)


 Video Files (.mp4)
 Audio Files (.mp3)
 Compressed Files (.zip, .rar)

Opening and Closing a File (in Python) :


 File contents need to be in RAM for accessing.
 So, file opening operation gets contents in RAM.
 Also opening the file returns a reference to file.
 File reference is used in different operations on file.
 File reference stores current position to read or write.
 There are two ways to open and close the files in python.

File Handling :

Open and Close :


 Open file Here Open() function is used to open the file.
 This function accepts the ‘filename’ and open mode to open the fileing”
Example: File handler=open(“filename”, ”open mode”, ”buffering”)
F=open(“myfile.txt”,”w”)
F is file handler or file object
myfile.text is the name and type of file.
w is the write-mode in which file is open .
 Buffering is the optional temporary integer which sets the size of memory for the
file.

File open Modes & Description


 r Opens a file for reading only.
 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.
 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.
 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.

 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.
 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.
 x To open the file in exclusive creation mode.
 The file creation failed if file already exists.

Close file
 The file which is open must be closed using close() function/method.
 If the file is not closed then data in it may be corrupted or deleted I some cases.
 Also the memory occupied by the file is not freed.
File_ref.close()
f.close()

File handling methods


Read()
Write()
Writelines()
Readlines()
List()
Seek()
Tell()

#python program to read and display it’s content


f=open('d:\\test.txt',"r") #open file in read mode
print(f.read()) #read file and display it’s contents
f.close()

# Python code to illustrate read() mode character wise


file = open(" d:\\test.txt ", "r")
print(file.read(6))
f.close()

#python program to Write and display it’s content


file = open(‘d:\\test1.txt ','w')
file.write("This is the write command")
file.write("It allows us to write in a particular file")
file.close()

# Python code to illustrate write() mode character wise


f = open('d:\\test1.txt ','w+')
f.write("This is the write command")
f.write("\n It allows us to write in a particular file")
f.write("\n\n\nPython programming")
f.close()

Seek() and tell() method


Seek method() used to change file pointer position and tell() method returns the current
position of file pointer
file_object.seek(offset[,whence])
• offset − This is the position of the read/write pointer within the file.
• whence − This is optional and defaults to 0 which means absolute file positioning, other
values are 1 which means seek relative to the current position and 2 means seek relative to the
file's end.

#Python program using seek() and tell() methods


F=open(‘d:\\test3.txt’,’r’)
print(“\tthe contents of the file are…”)
print(F.read())
Print(‘the current position of file pointer is: ‘)
Print(F.tell())
F.seek(0)
Print(‘Now the position of file cursor is:’)
Print(F.tell())

You might also like