Saturday, January 29, 2022

How to read and write data to Text or CSV Files in Python Program

How to Create, Open and Write data to Text or CSV Files in Python Program
The Python provides a wide range of inbuilt functions for creating, writing and reading files. In general, there are two types of files that can be handled in python, normal text files and binary files ( those written in binary language, 0s and 1s).

Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character (‘\n’) in python by default.
Binary files: In this type of file, there is no terminator for a line and the data will be stored after converting it into machine understandable binary language.

The files can be accessed in different modes like, read(r), write(w), read and write(r+, w+) and the appending mode(a, a+) as defined below:

File access modes::
The text or csv files can accessed using the below modes:
Read Only (‘r’) : In this mode, the file will be open only for reading. The cursor is positioned at the beginning of the file to read. If the file does not exists, it raises I/O error. This is the default mode in which file is opened.
Read and Write (‘r+’) : In this mode the file will be open for reading and writing. The cursor will be positioned at the beginning of the file. It raises I/O error if the file does not exists. 
It partially overrides(till the space occupied by the new data) existing data from the beginning of the file  

Write Only (‘w’) : In this mode, the file will be open only for writing. For existing file, the data is truncated and over-written. The cursor will be positioned at the beginning of the file. It creates the new file if the file does not exists.
Write and Read (‘w+’) : In this mode, the file will be open for reading and writing. For existing file, data will be truncated and over-written. The cursor will be positioned at the beginning of a file while reading or writing. It creates the new file if the file does not exists.

Append Only (‘a’) : In this mode, the file will be open for appending(writing). The file is created if it does not exist. The cursor is positioned at the end of the file for writing data. The data being written will be inserted at the end, after the existing data.
Append and Read (‘a+’) : In this mode the file will be open for appending(writing) and reading. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.

Create('x'): In this mode the file will be open for exclusive creation(and writing). It throws a File Exists Error if the file already exists.

The binary files can accessed using the below modes:
Read Only('rb') : In this mode, the file will be open for reading only in binary format. The file pointer is placed at the beginning of the file.
Read and Write('rb+') : In this mode the file will be open for both reading and writing in binary format.
Write and Read('wb+') : In this mode the file will be open 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.

Append('ab') : In this mode the file will be open 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.
Append and Read('ab+') : In this mode the file will be open 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.

Examples:
In the following examples we will look at working with the Text Files(.txt). We can process the CSV files in the same manner.
# ------------------------------------------------------------------------------------------------------------------------ #
import os
import sys
import csv
print("Python Version: ", sys.version)
# ------------------------------------------------------------------------------------------------------------------------ #
# Defining Folder and File path:
folder_path = 'C:\\Users\\write\\PycharmProjects\\pythonProject\\PyLab1\\File_Operations\\Input\\'
txtFile_name = "txtSample_Input.txt"
txtFile = folder_path + txtFile_name

# Current working Excel File name
print('The Text File name:\n', os.path.basename(txtFile)) 
# Current working Text File directory
print('The Text File Directory name:\n', os.path.dirname(txtFile)) 
# Current working Text File full path 
print('The full path of the Text File:\n', os.path.abspath(txtFile))  
# ------------------------------------------------------------------------------------------------------------------------ #
# Creating(x) a .txt file in given path and write a string:
myTXT_File = open(txtFile, "x", encoding="utf-8")
str1 = "The sample product data:\n"
myTXT_File.write(str1)
myTXT_File.close()

# Result:

# Create (w+) a Text file in the given path
myTXT_File = open(txtFile, "w+", encoding="utf-8")
myTXT_File.close()

# Write(w+) a string to an existing Text file
str1 = "The sample product data:\n"
myTXT_File = open(txtFile, "w+", encoding="utf-8")
myTXT_File.write(str1)
myTXT_File.close()

# Result:
The result will be same as above. In this 'w+' mode, it opens an existing file if exists, otherwise it create a new file and then write and overrides the existing data.
# ------------------------------------------------------------------------------------------------------------------------ #
# Write(r+) list of strings to an existing Text file using writelines() method:
lst1 = ["The list of Products:\n", "Apple \n", "Mango \n", "Banana\n"]

myTXT_File = open(txtFile, "r+")
myTXT_File.writelines(lst1)

# Read and Write(r+) the list data to and existing Text file using .join method
with open(txtFile, "r+") as file_obj:
    lst_data = "\n".join(lst1)
    file_obj.write(lst_data)

# Result:
As we know the 'r+' mode will read and write(and partial overriding the existing data). Since the newly written data occupied more space than the existing string(The sample product data) , it is overridden completely.

                              
# ------------------------------------------------------------------------------------------------------------------------ #
# Read and Append the list data to an existing Text file using for loop
lst_wk = ['The list of weeks:', '1', 'Mon', '2', 'Tue', '3', 'Wed', '4', 'Thu']
with open(txtFile, "a+") as file_obj:
    # looping through each list element
    for element in lst_wk:
        # Writing(Appending) data to file line by line
        file_obj.write('%s\n' % element)
    file_obj.flush()  #clearing the buffer memory before closing the file
file_obj.close()  #closing the file

# Result:
# ------------------------------------------------------------------------------------------------------------------------ #
# Writing the list of records(tuples) data to an existing Text file using for loop
ds1 = [('Prod_Id', 'Prod_Name', 'Price'),
       (124, 'Apple', 45),
       (125, 'Banana', 10),
       (126, 'Orange', 35)
       ]

file_obj = open(txtFile, "w+", encoding="utf-8")
for rec in ds1:
    line = ' '.join(str(k) for k in rec)
    file_obj.write(line + '\n')

file_obj.flush()  #clearing the buffer memory before closing the file
file_obj.close()  #closing the file

# Result:
As we know the 'w+' mode will read and write data to an existing file by overriding the existing data. It creates a new file if it does not exist.
# ------------------------------------------------------------------------------------------------------------------------ #
# Reading the entire data in the file:
file_obj = open(txtFile, "r", encoding="utf-8")
ds1 = file_obj.read()
print(ds1)

# Result:
Prod_Id Prod_Name Price
124 Apple 45
125 Banana 10
126 Orange 35

# Reading a specific line from a File:
line_num = 3
file_obj = open(txtFile, "r", encoding="utf-8")
cur_line = 1
print('The record from 3rd line is:')
for line in file_obj:
    if (cur_line == line_num):
        print(line)
        break
cur_line = cur_line + 1

# Result:
The record from 3rd line is:
125 Banana 10

# Reading the records line by line from a Text File:
file_obj = open(txtFile, "r", encoding="utf-8")
print('The list of records from input file:')
print(file_obj.readline())
print(file_obj.readline())
print(file_obj.readline())

# Result:
The list of records from input file:
Prod_Id Prod_Name Price
124 Apple 45
125 Banana 10

# Reading all the records/lines(incl. new line chars) from a Text File:
file_obj = open(txtFile, "r", encoding="utf-8")
print('The list of records from input file:')
print(file_obj.readlines())

# Result:
The list of records from input file:
['Prod_Id Prod_Name Price\n', '124 Apple 45\n', '125 Banana 10\n', '126 Orange 35\n']

# Reading first 5 characters from a file:
file_obj = open(txtFile, "r", encoding="utf-8")
ds1 = file_obj.read(5)
print(ds1)

file_obj.flush()  #clearing the buffer memory before closing the file
file_obj.close()  #closing the file

# Result:
The first 5 characters from input file:
Prod_

#--------------------------------------------------------------Thanks--------------------------------------------------------------#

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog