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 storage can be categorized into three primary types:
1. Text Files: These files are organized as a series of lines, with each line comprising a series of characters.
2. Binary Files: This type refers to any file format that isn’t recognized as a text file.
3. CSV Files: Standing for Comma Separated Values, these files are used to store data in a tabular form..

 

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.

 

 

 





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’ opens a file for reading and places the file pointer at the beginning. This is the default mode.
  • ‘r+’ opens a file for both reading and writing, placing the file pointer at the beginning of the file.
  • ‘w’ opens a file solely for writing. If the file exists, it is overwritten. If the file does not exist, a new file is created for writing.
  • w+’ opens a file for both writing and reading. It overwrites the file if it exists or creates a new one if it does not.
  • ‘rb’ opens a file for reading in binary format, placing the file pointer 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. It overwrites the file if it exists or creates a new one if it does not.
  • ‘a’ opens a file for appending, placing the file pointer at the end of the file if it exists, thus opening the file in append mode. If the file does not exist, a new file is created for writing.
  • ‘ab’ opens a file for appending in binary format, placing the file pointer at the end of the file if it exists, thus opening the file in append mode. If the file does not exist, a new file is created for writing.
  • ‘a+’ opens a file for both appending and reading, placing the file pointer at the end of the file if it exists. The file opens in append mode. If the file does not exist, a new file is created for reading and writing.
  • ‘ab+’ opens a file for both appending and reading in binary format, placing the file pointer at the end of the file if it exists. The file opens in append mode. If the file does not exist, it creates a new file for reading and writing.
  • ‘x’ opens a file exclusively for 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

 

 

 

Absolute and Relative Paths:

 

  1. Absolute Paths:

    • An absolute path specifies the exact location of a file or directory from the root directory.
    • It includes the entire path hierarchy, starting from the root and traversing down to the desired file or folder.
    • In Unix-like systems (including Linux), an absolute path typically begins with a forward slash (/). For example:
      • /home/user/documents/myfile.txt
      • /var/www/html/index.html
    • In Windows, an absolute path often starts with a drive letter (e.g., C:\) followed by the full path. For example:
      • C:\Users\Username\Documents\myfile.txt
      • D:\Projects\website\index.html
  2. Relative Paths:

    • A relative path specifies the location of a file or directory relative to the current working directory.
    • It does not start from the root directory; instead, it assumes a starting point within the directory structure.
    • Common symbols used in relative paths:
      • . (dot): Represents the current directory.
      • .. (double dot): Represents the parent directory.
    • Examples of relative paths:
      • If the current directory is /home/user/documents/:
        • ./myfile.txt refers to the file myfile.txt in the current directory.
        • ../images/pic.jpg refers to the file pic.jpg in the images directory one level up.
      • If the current directory is C:\Users\Username\Documents\:
        • .\myfile.txt refers to the file myfile.txt in the current directory.
        • ..\Projects\website\index.html refers to the index.html file in the website directory one level up.

Remember that absolute paths are fixed and do not change based on the current working directory, while relative paths adapt to the context of where they are used. Both types have their use cases, depending on the situation. 

 

 

error: Content is protected !!