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

Unit 2 Python

The document provides an overview of file handling in Python, detailing the use of the open() function and its various modes for reading, writing, and appending files. It includes examples of reading files line by line, handling exceptions, and using libraries like Pandas and openpyxl for working with Excel files. Additionally, it emphasizes the importance of closing files after operations and demonstrates how to perform basic arithmetic operations within Excel spreadsheets using Python.

Uploaded by

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

Unit 2 Python

The document provides an overview of file handling in Python, detailing the use of the open() function and its various modes for reading, writing, and appending files. It includes examples of reading files line by line, handling exceptions, and using libraries like Pandas and openpyxl for working with Excel files. Additionally, it emphasizes the importance of closing files after operations and demonstrates how to perform basic arithmetic operations within Excel spreadsheets using Python.

Uploaded by

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

File handling is an important part of any web application.

Python has several functions for creating, reading, updating, and deleting
files.

File Handling
The key function for working with files in Python is the open() function.

The open() function takes two parameters; filename, and mode.

There are four different methods (modes) for opening a file:

"r" - Read - Default value. Opens a file for reading, error if the file does not exist

"a" - Append - Opens a file for appending, creates the file if it does not exist

"w" - Write - Opens a file for writing, creates the file if it does not exist

"x" - Create - Creates the specified file, returns an error if the file exists

In addition you can specify if the file should be handled as binary or text mode

"t" - Text - Default value. Text mode

"b" - Binary - Binary mode (e.g. images)

Syntax
To open a file for reading it is enough to specify the name of the file:

f = open("demofile.txt")

The code above is the same as:

f = open("demofile.txt", "rt")

Because "r" for read, and "t" for text are the default values, you do not need to
specify them.

Note: Make sure the file exists, or else you will get an error.
Open a File on the Server
Assume we have the following file, located in the same folder as Python:

demofile.txt

Hello! Welcome to demofile.txt


This file is for testing purposes.
Good Luck!

To open the file, use the built-in open() function.

The open() function returns a file object, which has a read() method for reading
the content of the file:

f = open("demofile.txt", "r")
print(f.read())

If the file is located in a different location, you will have to specify the file path,
like this:

Open a file on a different location:

f = open("D:\\myfiles\welcome.txt", "r")
print(f.read())

Read Only Parts of the File


By default the read() method returns the whole text, but you can also specify
how many characters you want to return:

Example
Return the 5 first characters of the file:

f = open("demofile.txt", "r")
print(f.read(5))

Read Lines
You can return one line by using the readline() method:

Example
Read one line of the file:

f = open("demofile.txt", "r")
print(f.readline())

By calling readline() two times, you can read the two first lines:

Example
Read two lines of the file:

f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())

By looping through the lines of the file, you can read the whole file, line by line:

Example
Loop through the file line by line:

f = open("demofile.txt", "r")
for x in f:
print(x)

Close Files
It is a good practice to always close the file when you are done with it.

Example
Close the file when you are finished with it:

f = open("demofile.txt", "r")
print(f.readline())
f.close()

Note: You should always close your files. In some cases, due to buffering,
changes made to a file may not show until you close the file

Local machine

file_path = 'C:/Users/Admin/Desktop/python.txt'
try:
with open(file_path, 'r') as file:
contents = file.read()
print(contents)
except FileNotFoundError:
print(f"Error: File not found at {file_path}")
except Exception as e:
print(f"An error occurred: {e}")

Google colab
Upload the file, copy the path and run

# prompt: to read a text file in desktop

from google.colab import drive


drive.mount('/content/drive')
file_path = '/content/drive/MyDrive/python.txt'

try:
with open(file_path, 'r') as file:
contents = file.read()
print(contents)
except FileNotFoundError:
print(f"Error: File not found at {file_path}")
except Exception as e:
print(f"An error occurred: {e}")

Notice that the open() function takes two input parameters: file path (or file name if the
file is in the current working directory) and the file access mode. There are many modes
for opening a file:
● open('path','r'): opens a file in read mode
● open('path',w'): opens or creates a text file in write mode
● open('path',a'): opens a file in append mode
● open('path','r+'): opens a file in both read and write mode
● open('path',w+'): opens a file in both read and write mode
● open('path',a+'): opens a file in both read and write mode

After opening the file with the read mode, you can also use the following function to
access or examine the Information stored in the file:
● .read(): This function reads the complete information from the file unless a
number is specified. Otherwise, it will read the first n bytes from the text files.
● .readline(): This function reads the information from the file but not more than
one line of information unless a number is specified. Otherwise, it will read the
first n bytes from the text files. It is usually used in loops
● .readlines() – This function reads the complete information in the file and prints
them as well in a list format

Example for uploading

import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from google.colab import files
uploaded = files.upload()
import io
df = pd.read_excel(io.BytesIO(uploaded['des.xlsx']))
df.shape

Reading Excel Files

Pandas library also has a function, read_excel(), to read Excel files:

dataframe = pd.read_excel('path-to-file') # Full path to the txt file,


or simply the file name if the file is in
# your current working
directory
It is often the case where Excel file can contain multiple sheets. You can read data from
any sheet by providing its name in the sheet_name parameter in the read_excel()
function:

dataframe = pd.read_excel('path-to-file', sheet_name='sheet-name')


Sometimes, you might want to use one column of data for Analysis. To do this, you can
get the column data and convert it into a list of values.

print(dataframe['column-name'].tolist())

Modes of Opening a File


When opening a file in Python, you need to specify the mode in which you want to open
it. The most common modes are:

- r: Opens the file for reading (default mode).


- w: Opens the file for writing. If the file exists, its contents will be deleted. If the file
doesn't exist, it will be created.
- a: Opens the file for appending. If the file exists, new data will be appended to the end
of the file. If the file doesn't exist, it will be created.
- x: Opens the file for exclusive creation. If the file exists, the operation will fail.
- b: Opens the file in binary mode.
- t: Opens the file in text mode (default).
- +: Opens the file for updating (reading and writing).

Reading a File
Here are a few ways to read a file in Python:

Method 1: Reading a File Line by Line

file = open('file.txt', 'r')


for line in file:
print(line.strip())
file.close()

Method 2: Reading a File into a List

file = open('file.txt', 'r')


lines = file.readlines()
file.close()
print(lines)

Method 3: Reading a File into a String


file = open('file.txt', 'r')
content = file.read()
file.close()
print(content)

Method 4: Using the with Statement


The with statement is a more Pythonic way to open and close files. It automatically
closes the file when you're done with it.

with open('file.txt', 'r') as file:


content = file.read()
print(content)

Note: Make sure to replace 'file.txt' with the actual path to your file.

Reading an excel file using Python using Pandas

In this method, We will first import the Pandas module then we will use
Pandas to read our excel file. You can read more operations using the excel
file using Pandas
# import pandas lib as pd
import pandas as pd
# read by default 1st sheet of an excel file
dataframe1 = pd.read_excel('C:\\Users\\Admin\\Desktop\\python1.xlsx')
print(dataframe1)

You can read an Excel file in Python using the openpyxl library, which
supports both .xlsx and .xls formats:

import openpyxl
# Load workbook
wb = openpyxl.load_workbook('C:\\Users\\Admin\\Desktop\\python1.xlsx')
# Select active sheet (optional)
sheet = wb.active
# Access cell values
cell_value = sheet['A1'].value
print(cell_value)
# Close workbook when done
wb.close()

Replace ‘path/to/your/excel_file.xlsx’ with the actual path to your Excel file.

How to open an Excel file from Python?

To open and manipulate Excel files, you can also use the xlrd library, which
supports .xls files (not .xlsx):

import xlrd
# Open Excel file
workbook = xlrd.open_workbook('path/to/your/excel_file.xls')
# Access sheet by index or name
sheet = workbook.sheet_by_index(0) # Access first sheet
# OR
# sheet = workbook.sheet_by_name('Sheet1') # Access sheet by
name
# Read cell value
cell_value = sheet.cell_value(0, 0) # Access cell A1
print(cell_value)
# Close workbook when done
workbook.close()

How to read an xlsx file in Python using Pandas?

Pandas provides a convenient way to read Excel files directly into a


DataFrame:

import pandas as pd
# Read Excel file into DataFrame
df = pd.read_excel('path/to/your/excel_file.xlsx')
# Display DataFrame
print(df.head())

Pandas’ read_excel() function can handle both .xls and .xlsx formats.

How to read xlsx file in Python using CSV?

If you prefer working with CSV format data extracted from an Excel file, you
can convert an Excel file into CSV format using Pandas:

import pandas as pd

# Read Excel file into DataFrame

df = pd.read_excel('path/to/your/excel_file.xlsx')
# Export DataFrame to CSV

df.to_csv('output_file.csv', index=False)

This converts the Excel data into a CSV file named output_file.csv in the
current directory.

import openpyxl
# Load workbook
wb = openpyxl.load_workbook('C:\\Users\\Admin\\Desktop\\python2.xlsx')
# Select active sheet (optional)
sheet = wb.active
# Access cell values
cell_value = sheet['A3'].value
print(cell_value)
# Close workbook when done
wb.close()

rose

In [8]:
# import pandas lib as pd
import pandas as pd
# read by default 1st sheet of an excel file
dataframe1 = pd.read_excel('C:\\Users\\Admin\\Desktop\\python2.xlsx')
print(dataframe1)

flower colour
0 jasmine white
1 rose pink
2 marygold yellow

In [7]:
import pandas as pd
# Read Excel file into DataFrame
df = pd.read_excel('C:\\Users\\Admin\\Desktop\\python2.xlsx')
# Display DataFrame
print(df.head())
flower colour
0 jasmine white
1 rose pink
2 marygold yellow

In [15]:
import pandas as pd
# Read Excel file into DataFrame
df = pd.read_excel('C:\\Users\\Admin\\Desktop\\python2.xlsx')
# Export DataFrame to CSV
df.to_csv('C:\\Users\\Admin\\Desktop\\output_file.csv', index=False)

In [16]:
from openpyxl import Workbook
workbook = Workbook()
workbook.save(filename='C:\\Users\\Admin\\Desktop\\python4.xlsx')

In [23]:
# import openpyxl module
import openpyxl
wb = openpyxl.load_workbook('C:\\Users\\Admin\\Desktop\\python4.xlsx')
#sheet = wb.active
data = (
(1, 2, 3),
(4, 5, 6)
)
for row in data:
sheet.append(row)
wb.save('C:\\Users\\Admin\\Desktop\\python4.xlsx')

In [ ]:

In this example, a new blank Excel workbook is generated using the


openpyxl library’s Workbook() function, and it is saved as “sample.xlsx”
with the save() method. This code demonstrates the fundamental steps for
creating and saving an Excel file in Python.
from openpyxl import Workbook
workbook = Workbook()
workbook.save(filename="sample.xlsx")

Output:

After creating an empty file, let’s see how to add some data to it using
Python. To add data first we need to select the active sheet and then using
the cell() method we can select any particular cell by passing the row and
column number as its parameter. We can also write using cell names. See
the below example for a better understanding.

Example:

In this example, the openpyxl module is used to create a new Excel


workbook and populate cells with values such as “Hello,” “World,”
“Welcome,” and “Everyone.” The workbook is then saved as “sample.xlsx,”
illustrating the process of writing data to specific cells and saving the
changes
# import openpyxl module
import openpyxl
wb = openpyxl.Workbook()
sheet = wb.active
c1 = sheet.cell(row=1, column=1)
# writing values to cells
c1.value = "Hello"
c2 = sheet.cell(row=1, column=2)
c2.value = "World"
c3 = sheet['A2']
c3.value = "Welcome"
# B2 means column = 2 & row = 2.
c4 = sheet['B2']
c4.value = "Everyone"
wb.save("sample.xlsx")

Output:

Refer to the below article to get detailed information about writing to excel.

Append data in excel using Python


In the above example, you will see that every time you try to write to a
spreadsheet the existing data gets overwritten, and the file is saved as a
new file. This happens because the Workbook() method always creates a
new workbook file object. To write to an existing workbook you must open
the file with the load_workbook() method. We will use the above-created
workbook.

Example:

In this example, the openpyxl module is employed to load an existing Excel


workbook (“sample.xlsx”). The program accesses cell ‘A3’ in the active sheet,
updates its value to “New Data,” and then saves the modified workbook back
to “sample.xlsx.”

# import openpyxl module


import openpyxl
wb = openpyxl.load_workbook("sample.xlsx")
sheet = wb.active
c = sheet['A3']
c.value = "New Data"
wb.save("sample.xlsx")

Output:

We can also use the append() method to append multiple data at the end of
the sheet.

Example:

In this example, the openpyxl module is utilized to load an existing Excel


workbook (“sample.xlsx”). A two-dimensional data structure (tuple of tuples)
is defined and iteratively appended to the active sheet, effectively adding
rows with values (1, 2, 3) and (4, 5, 6).

# import openpyxl module


import openpyxl
wb = openpyxl.load_workbook("sample.xlsx")
sheet = wb.active
data = (
(1, 2, 3),
(4, 5, 6)
)
for row in data:
sheet.append(row)
wb.save('sample.xlsx')

Output:

Arithmetic Operation on Spreadsheet


Arithmetic operations can be performed by typing the formula in a particular
cell of the spreadsheet. For example, if we want to find the sum then =Sum()
formula of the excel file is used.

Example:

In this example, the openpyxl module is used to create a new Excel


workbook and populate cells A1 to A5 with numeric values. Cell A7 is
assigned a formula to calculate the sum of the values in A1 to A5.

# import openpyxl module

import openpyxl

wb = openpyxl.Workbook()

sheet = wb.active

# writing to the cell of an excel sheet

sheet['A1'] = 200

sheet['A2'] = 300

sheet['A3'] = 400

sheet['A4'] = 500

sheet['A5'] = 600
sheet['A7'] = '= SUM(A1:A5)'

# save the file

wb.save("sum.xlsx")

Output:

Refer to the below article to get detailed information about the Arithmetic
operations on Spreadsheet.

Adjusting Rows and Column


Worksheet objects have row_dimensions and column_dimensions attributes
that control row heights and column widths. A sheet’s row_dimensions and
column_dimensions are dictionary-like values; row_dimensions contains
RowDimension objects and column_dimensions contains ColumnDimension
objects. In row_dimensions, one can access one of the objects using the
number of the row (in this case, 1 or 2). In column_dimensions, one can
access one of the objects using the letter of the column (in this case, A or B).

Example:

In this example, the openpyxl module is used to create a new Excel


workbook and set values in specific cells. The content “hello” is placed in cell
A1, and “everyone” is placed in cell B2. Additionally, the height of the first
row is set to 70 units, and the width of column B is set to 20 units.

# import openpyxl module

import openpyxl

wb = openpyxl.Workbook()

sheet = wb.active

# writing to the specified cell

sheet.cell(row=1, column=1).value = ' hello '


sheet.cell(row=2, column=2).value = ' everyone '

# set the height of the row

sheet.row_dimensions[1].height = 70

# set the width of the column

sheet.column_dimensions['B'].width = 20

# save the file

wb.save('sample.xlsx')

Output:

Merging Cells

A rectangular area of cells can be merged into a single cell with the
merge_cells() sheet method. The argument to merge_cells() is a single string
of the top-left and bottom-right cells of the rectangular area to be merged.

Example:

In this example, the openpyxl module is employed to create a new Excel


workbook. The program merges cells A2 to D4, creating a single cell
spanning multiple columns and rows, and sets its value to ‘Twelve cells join
together.’ Additionally, cells C6 and D6 are merged, and the text ‘Two merge
cells.’ is placed in the resulting merged cell.

import openpyxl

wb = openpyxl.Workbook()

sheet = wb.active

sheet.merge_cells('A2:D4')

sheet.cell(row=2, column=1).value = 'Twelve cells join together.'


# merge cell C6 and D6

sheet.merge_cells('C6:D6')

sheet.cell(row=6, column=6).value = 'Two merge cells.'

wb.save('sample.xlsx’)

You might also like