CBSE CS

cbse cs logo

Home

Data files in Python

Topic Covered

  • Introduction to files, types of files (Text file, Binary file, CSV file),

  • relative and absolute paths

  • Text file: opening a text file, text file open modes (r, r+, w, w+, a, a+), closing a text file,

  • opening afile using with clause, writing/appending data to a text file using write() and writelines(),

  • reading from a text file using read(), readline() and readlines(),

  • seek and tell methods, manipulation of data in a text file

Python Text files:

File:- A file is a collection of related data stored in computer storage for future data retrieval.

Types of  Data Files:
Data files can be stored in three ways:
I. Text Files: Text files are structured as a sequence of lines, where each line includes a sequence of characters.
II. Binary Files: A binary file is any type of file that is not a text file.
III. csv Files: Comma Separated Values  file.1. Text Files: Text files are structured as a sequence of lines, where each line includes a sequence of characters.

2. Binary Files: A binary file is any type of file that is not a text file.  Binary files are used to store binary data such as images, video files, audio files etc. They store data in the binary format (0‘s and 1‘s). In Binary files there is no delimiter for a line. To open files in binary mode, when specifying a mode, add ‘b’ to it. Pickle module can be imported to write or read data in a binary file.

3. CSV (Comma Separated Values) is a file format for data storage which looks like a text file. The information is organized with one record on each line and each field is separated by comma.

CSV File Characteristics
• One line for each record
• Comma separated fields
• Space-characters adjacent to commas are ignored
• Fields with in-built commas are separated by double quote characters.




Text Files

Binary Files

CSV files

Stores information in ASCII characters.

Stores information in the same format  which the information is held in memory.

Stores information in ASCII characters.

Each line of text is terminated with a  special character known as EOL (End of Line ).

No delimiters are used for a line.

It terminates a line automatically when the delimiter is not used after data.

Some internal translations take place when

this EOL character is read or written.

No translation occurs in binary files.

It consists of plain text with a list of data with a delimiter.

Slower than binary files.

Binary files are faster and easier for a program to read and write the text files.

Slower than binary files.

Any text editors like notepad can be used to read them.

No specific programs can be used to read them, python provides functions to read data.

It can be read using text editors like notepads and spreadsheet software.





Need of File Handling:

  • File handling is an important part of any web application
  •  To store the data in secondary storage.
  •  To Access the data fast.
  •  To perform Create, Read, Update, Delete operations easily.

 

 

Absolute and relative path:

Absolute path:

This is the path of a file in a system which starts from the root of the file system. It describes the location of a file regardless of the current working directory.
Example: “D:\\python programs\\Book.txt”

Relative path:

This is the file path without slash. It describes the location of a file in relative to the current working directory. Generally ’.’ represents the absolute path of the folder where the py file is currently located(parent folder), and ’..’ represents

The absolute path of the upper level folder(parent of parent), of the parent folder where the py file is currently located
Example:

  • “Book.txt”
  • ..\\test1\y.txt
  • .\y.txt
  • “..\\x.txt”




Basic operations performed on a data file are:

  • Opening a file
  • Reading data from the file
  • Writing data in the file
  • Append the data to a file
  • Delete a file
  • Closing a file

Opening Files

  • To handle data files in python, we need to have a file object ( a kindof variable).
  • Object can be created by using open() function or file() function.
  • Using this function a file object is created which is then used for accessing various methods and functions available for file manipulation.





Syntax of open() function is

file_object =  open( filename  , access_mode )

open() requires three arguments to work,

first 🙁 filename ) is the name of the file on secondary storage.

second: (access_mode) describes how file will be used throughout  the program. This is an optional parameter and the default access_mode ( r ) is reading.

Example:
To open a file for reading it is enough to specify the name of the file:
f = open(“book.txt”)
The code above is the same as:
f = open(“book.txt”, “r”)
Where “r” for read mode, and “t” for text are the default values, you do not need to specify them.




With statement
The with statement will automatically close the file after the nested block of code.
with open(“filename”) as file:
         do something with data
         with open(‘abc.txt’, ‘w’) as f:
        f.write(‘Hi there!’)

Python file modes

Don’t confuse, read about every mode as below.

  • r for reading – 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 will be 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, it creates a new file for reading and writing.
  • rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file.
  • rb+ Opens a file for both reading and writing in binary format.
  • 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, it 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.
  • 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.
  • x open for exclusive creation, failing if the file already exists (Python 3)





Read the data from a file:
There are 3 types of functions to read data from a file.

  • read( ) : reads n bytes. if no n is specified, reads the entire file.
  • readline( ) : Reads a line. if n is specified, reads n bytes.
  • readlines( ): Reads all lines and returns a list.

Steps to read data from a file:

• Create and Open a file using open( ) function
• use the read( ), readline( ) or readlines( ) function
• print/access the data
• Close the file.


Using Read() function

Read n no of bytes and returns it in form of a string. if n is not specified, reads the entire file

The method read() reads at most size bytes from the file. If the read hits EOF before obtaining size bytes, then it reads only available bytes.

Syntax

data = File_object.read([n])

Parameters:  size − This is the number of bytes to be read from the file.

Return Value   :  open( ) method returns the bytes read in string.

Example

fo = open(“foo.txt”, “rw+”)

print( “Name of the file: “, fo.name )

String_data = fo.read(10)

print( String_data )

fo.close()   # Close opend file

2 Write a program to read a text file “abc.text” using read()
with open(“abc.txt”) as e: # or open(“abc.txt”,”r”)
     g=e.read()
     print(g)
or
e=open(“abc.txt”,”r”)
l=e.read()
print(l)
e.close()




3. Write a program to read the text file “abc.txt” and return the 10 first characters of the file:
e=open(“abc.txt”,”r”)
l=e.read(10)
print(l)
e.close()


 readline( )  Function

The method readline()reads one entire line from the file. A trailing  newline character is kept in the string. If the size argument is present and non-negative, it is a maximum byte count including the trailing newline and an incomplete line may be returned.

Syntax:

String_data = fileObject.readline( size );

Parameters  :  size − This is the number of bytes to be read from the line.

Return Value : This method returns the string line read from the file.

Write a program to read 1st line from “abc.txt” and display on screen.
e=open(“abc.txt”,”r”)
l=e.readline()
print(l)
e.close()

Give the output of the following program
with open(“abc.txt”,”w”) as f:
     L=[“sunday\n”,”monday\n”,”tuesday\n”]
     f.writelines(L)

e=open(“abc.txt”,”r”)
l=e.readline(10)
print(l)
e.close()





Modify the above program and read entire file using readline function
Ans. e=open(‘abc.txt’,’r’)
l=e.readline()
while(l):
     print(l,end=””)
     l=e.readline()
e.close()





Using Readlines()

Reads all the lines and return them as each line a string element in a list.

Syntax:

list_variable = File_object.readlines( )

return type: list

1. Write a program to read text file “abc.text” using readlines function
with open(“abc.txt”,”r”) as e:
     l=e.readlines()
     print(l)

2. Modify the above program and print the list using for loop
with open(“abc.txt”,”r”) as e:
     l=e.readlines()
     for i in l:
         print(i)


Let a text file “Book.txt” has the following text:
“Python is interactive language. It is case sensitive language.
It makes the difference between uppercase and lowercase letters.
It is official language of google.”

Example-1: Program using read( ) function:
fin=open(“D:\\python programs\\Book.txt”,’r’)

str=fin.read( )
print(str)
fin.close( )
OUTPUT:
Python is interactive language. It is case sensitive language.
It makes the difference between uppercase and lowercase letters.
It is official language of google.

fin=open(“D:\\python programs\\Book.txt”,’r’)
str=fin.read(10)
print(str)
fin.close( )
OUTPUT:
Python is

Example-2: using readline( ) function:
fin=open(“D:\\python programs\\Book.txt”,’r’)
str=fin.readline( )
print(str)
fin.close( )

OUTPUT:
Python is interactive language. It is case sensitive language.





Example-3: using readlines( ) function:
fin=open(“D:\\python programs\\Book.txt”,’r’)
str=fin.readlines( )
print(str)
fin.close( )

OUTPUT:
[‘Python is interactive language. It is case sensitive language.\n’, ‘It makes the
difference between uppercase and lowercase letters.\n’, ‘It is official language of google.’]


Writing in Files – write()

The write() method writes any string to an open file. It is important to note that Python strings can have binary data and not just text.

The write() method does not add a newline character (‘\n’) to the end of the string −

Syntax :

fileObject.write(string);

Here, passed parameter is the content to be written into the opened file.

Example:

# Open a file

fo = open(“foo.txt”, “wb”)

fo.write( “Python is a great language. \nYeah its great!! \n”);

# Close opend file

fo.close()

#WAP to copy content of one file into another.

file1 =open(‘data4.txt’,’w’)

file2= open(‘sss.py’,’r’)

data = file2.read()

file1.write(data)

file1.close()

file2.close()

Writing in Files – writelines()

The method writelines() writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value.

Syntax

Following is the syntax for writelines() method −

fileObject.writelines( sequence )

Parameters

sequence − This is the Sequence of the strings. Sequence like list, tuple etc.

Return Value  : This method does not return any value.

#WAP to get roll number , name and marks of 5 students and store in the file.

file = open(‘data.txt’,’w’)

for i in range(5):

         r = input(“enter roll number”)

         name = input(“enter name”)

         marks = input(“enter marks”)

        s= ‘\n Roll number:’+r+’\t name: ‘+name+’\t marks: ‘+marks

        file.write(s)

file.close()

Delete a file:    To delete a file, you have to import the os module, and use

remove( ) function

import os
os.remove(“Book.txt”)


Tell() Function

Tell() method returns current position of file object. This method takes no parameters and returns an integer value

 Give the output of the following code
f=open(“C:\\abc.txt”,”r”)
f.seek(10)
t=f.tell()
print(“current position=”,t)
k=f.read()    # starts reading from the 10th character
print(k)
f.seek(5)
t=f.tell()
print(“current position=”,t)
k=f.read() # starts reading from the 5th character
print(k)
Ans. OUTPUT
current position= 10
s called an intelligent hub as it forwards the data packets only to the intended nodes.
current position= 5

Seek() Function:

The seek method is used to move the file pointer to a specified position.
seek has two parameters.

Syntax:

f.seek(offset, whence), where f is file pointer
Parameters:
Offset: Number of bytes to move (+ or -)
whence: This is optional and defaults to 0. It tells where to start from
Returns: Does not return any value

The first says how many the second parameter. The former is called offset and the latter is called whence.
A positive offset will move the pointer forward, a negative offset would move backward.

The whence can be any of the following values:
• os.SEEK_SET or 0 – sets the reference point at the beginning of the file (by default)
• os.SEEK_CUR or 1 – sets the reference point at the current file position
• os.SEEK_END or 2 – sets the reference point at the end of the file

error: Content is protected !!