You are on page 1of 50

Python Programming

(203105467)
Mr. Suman
Ch - 5Shekhar , Assistant
Database and Professor
File Operations

Computer Science & Engineering


CHAPTER-5
Database and File Operations
Unit-5 Database and File Operations
• Topics:
Database - Introduction, Connections, Executing queries,
Transactions Handling error, Importing data from Web and CSV
Need of a file. Opening, closing and read/write operations in file.
How Python handle files
Most of the files on our computer system are binary
files.
Binary files example
•Example:
•1. Document files: .pdf, .doc, .xls etc.
•2. Image files: .png, .jpg, .gif, .bmp etc.
•3. Video files: .mp4, .3gp, .mkv, .avi etc.
•4. Audio files: .mp3, .wav, .mka, .aac etc.
•5. Database files: .mdb, .accde, .frm, .sqlite etc.
•6. Archive files: .zip, .rar, .iso, .7z etc.
•7. Executable files: .exe, .dll, .class etc.
Text files example
Text files do not have a specific encoding it can be opened using
text editor.
Text files example
Example:
1. Web standards: html, XML, CSS, JSON etc.
2. Source code: c, app, js, py, java etc.
3. Documents: txt, tex, RTF etc.
4. Tabular data: csv, tsv etc.
5. Configuration: ini, cfg, reg etc
Database Connection
There are the following steps to connect a python application to our
database.

● Import mysql.connector module


● Create the connection object.
● Create the cursor object
● Execute the query
Database Connection
Import mysql.connector module

● To create a connection between the MySQL database and the


python application, the connect() method of mysql.connector
module is used.

● Pass the database details like HostName, username, and the


database password in the method call. The method returns the
connection object.
Database Connection
Import mysql.connector module

● Syntax:
● Connection-Object= mysql.connector.connect(host = <host-
name> , user = <username> , passwd = <password> )
Database Connection
Import mysql.connector module
● Example:
Database Connection
Import mysql.connector module
● Here, we must notice that we can specify the database name in
the connect() method if we want to connect to a specific
database.
Database Connection
Database Connection
Create the cursor object

● The cursor object can be defined as an abstraction specified in


the Python DB-API 2.0. It facilitates us to have multiple separate
working environments through the same connection to the
database. We can create the cursor object by calling the 'cursor'
function of the connection object. The cursor object is an
important aspect of executing queries to the databases.

● The syntax to create the cursor object is given below.


● <my_cur> = conn.cursor()
Database Connection
Create the cursor object
Executing queries
● Step 1 — Importing SQLite and Pandas
import sqlite3
import pandas as pd
● Step 2 — Connecting your database
● Using the connect() function allows you to create a database in
your environment. This helps users name their database to be
called in Python later on. The connect() function maintains the
connection while you are working with the database.
● An important thing to note is that while you are connected to
the database, other users will not be able to access it at the
same time. This is why it is essential to close() the connection
when you are done.
Executing queries
sql_connect = sqlite3.connect('factbook.db')
● Step 3 — Cursor Object
● The cursor() function is used to assist with executing our SQL
queries.
● cursor = sql_connect.cursor()
● Step 4 — Writing a Query
● The table from our factbook.db file is called facts.
● Save SQL Query as a string
● query = "SELECT * FROM factbook;"
Executing queries
● Execute the query using the cursor variable from earlier. This will
convert the results to tuples and store it as a local variable. To
get all the results we use fetchall().
● results = cursor.execute(query).fetchall()
● Step 5 — Running Query
● To run the query we saved early with pandas we do the
following.
● pd.read_sql_query(query,sql_connect)
Executing queries
● Output:
Executing queries
● Step 6 — Closing your connection
● Remember it is important to close your connection when you are
finished. Closing the connection will grant others access to the
database.
● sql_connect.close()
Executing queries
● Here is a good template for SQLite’s thought process.
1. connection open
2. transaction started
3. statement executes
4. transaction done
5. connection closed
Handling Errors
● Interacting with a database is an error prone process, so we
must always implement some mechanism to handle errors
gracefully.

● MySQLdb has MySQLdb.Error exception, a top level exception


that can be used to catch all database exception raised by
MySQLdb module.
Handling Errors
● Two main error in MySQLdb
● There are two categories of exception class in MySQLdb you
need to be aware:
○ DatabaseError.
○ InterfaceError.
Handling Errors
● DatabaseError: This exception raises when there is problem in
data processing, error in sql syntax, mysql internal problems. If a
connection is made and a problem arises then DatabaseError will
catch it. DatabaseError is further divided into 6 types: Data Error,
Internal Error, Integrity Error, Operational Error, Not Supported
Error, Programming Error.
● InterfaceError: When database connection fails for some reason,
MySQLdb will raise an InterfaceError. Note InterfaceError only
get raise when there is internal problem in connection to the
database, MySQLdb will not raise InterfaceError because of
wrong database name or password.
File operation
● Python programming language is capable of doing wonders and
with time we see a lot of its applications in various domains. One
of its wonders handling and organization of files which would
save a lot of time for us.
File operation
● A wide range of functions in Python is capable to cater the need
of file operations such as opening, reading, writing, creating files
et cetera. In the following guide, we will go through the basic yet
most necessary operations which are highly useful during every
file handling task.
● For performing file operations, a file needs to be opened first.
Then follows the necessary operations to be performed by the
user on our file. After all the desired operations are performed,
the file needs to be closed. Closing a file is necessary as it would
save the changes made on our file from the current session.
File operation
● Opening a File
● Reading a File
● Writing / Creating a File
● Closing a File
● Truncating a File
● Renaming a File
● Deleting a File
File operation
● Opening a File
● Files in Python can be opened with a built-in open() function.
Ideally, it takes two string arguments:
● 1. The file path including the file name and the extension we
want to open, is to be passed as a string
● 2. The mode in which we want to open the file, to be passed as a
string.
● Thus, the syntax for opening a file would look like this:
● open("", "")
File operation
● Opening a File
● Suppose if we have a file named myfile.txt on our desktop, it can
be opened by:
● open("C:Users/Suman/Desktop/myfile.txt")
● Unquestionably, if our current working directory of the Python
file is the same as our file (here desktop), there is no need to
specify the full path. In such a case, our open() function would
look like:
● open("myfile.txt")
File operation
● Opening a File
● Python has a wide range of file opening modes available by
default. These modes specify in which mode our file needed to
be opened.
● Let’s look at each of these modes:
● In ‘r’ mode, the file opens in the read mode. By default, if we
don’t specify the mode argument, it will be treated as read or ‘r’
mode. For Example, To open our file myfile.txt in ‘r’ mode:
● open("myfile.txt") or open("myfile.txt", "r")
File operation
● Opening a File
● In the ‘w’ mode, the file opens in write mode. It removes existing
content, if present, in the file. If the specified file doesn’t exist, it
creates one with the specified name. For Example,
● open("myfile.txt", "w")
● In the ‘a’ mode, the file opens in append mode. It adds content
to an existing file (or append at the end of the file). If the
specified file doesn’t exist, it creates one with the specified
name. It does not remove the existing content from the file. For
Example, To open our file myfile.txt in ‘a’ mode:
● open("myfile.txt", "a")
File operation
● Opening a File
● In the ‘r+’ mode, the file opens in the read & write mode. It does
not remove existing content, For Example, To open our file
myfile.txt in ‘r+‘ mode:
● open("myfile.txt", "r+")
● In the ‘w+’ mode, open the file in the read & write mode &
remove existing content. If the file doesn’t exist, it creates a new
one. It does not remove existing content & if a file doesn’t exist,
it creates a new one. For Example, To open our file myfile.txt in
‘w+’ mode:
● open("myfile.txt", "w+")
File operation
● Opening a File
● In the ‘a+’ mode, the file opens in the read & append mode. It
does not remove existing content & If the file doesn’t exist, it
creates a new one. For Example, To open our file myfile.txt in a+
mode:
● open("myfile.txt", "a+")
File operation
● Opening a File
● The modes discussed above are being used on a text file. To use
these modes for a binary file, we need to use a different
combination of file opening mode arguments. Using ‘b’ with any
mode, For example, ‘ab’, ‘rb’, ‘wb’, ‘rb+’, the file opens in binary
mode. It is used for non-textual files like image, sound,
executable (.exe) files.
● For Example, To open our file myfile.txt in b mode:
● open("myfile.txt", "rb")
File operation
● Opening a File
● Opening a file is not enough. This open function needs to be
stored into a variable as this will be used later for writing &
reading the file. Here we will assign a variable to our file:
● file = open("myfile.txt", "r")
File operation
● Reading a File
● Before start reading the file, as discussed, we must open the file
in ‘r’ mode, since we are going to read the contents of the file.
Let us assume that our file myfile.txt already has some textual
data present in it.
File operation
● Reading a File
● The .read() method to our variable file gives the content of the
file as output. We can also specify the number of characters we
want to read at once.
File operation
● Reading a File
● Let’s understand this with another example. Given our file
named myfile.txt having some content “Hello, how you all have
been doing?”. After opening a file, if we use print(file.read(6)), it
will first give “Hello,” as output & if use print(file.read(8)), it will
start reading 8 characters file from the point where the first read
function left off i.e. ” how you”
File operation

● Reading a File
● The .readline() method prints each line from our file, every time we
execute this function, until the end of the file is reached. This will
print the first two lines of our file, separated by a newline
character.
● The .readlines() method always gives a list of all the lines of our file
as list elements.
File operation

● Reading a File
● The .readline() method prints each line from our file, every time we
execute this function, until the end of the file is reached. This will
print the first two lines of our file, separated by a newline
character.
● The .readlines() method always gives a list of all the lines of our file
as list elements.
File operation
● Reading a File
● A major difference between the .read() method and the .readline()
method is, .read() method focuses on reading each character from
a file while the .readline() focuses on reading individual lines from a
file.
File operation
● Writing/Creating a File
● If we want to add content to our existing file, myfile.txt, first we
need to open it in the ‘w’ write mode and use the .write()
method on our file. The content to be written into the file needs
to be passed as a string. It’s a good practice to add a newline
character ‘n’ to add a line between sentences. This improves the
readability for the user and is also useful while using the
.readline() and .readlines() methods to distinguish between the
lines.
File operation
● Writing/Creating a File
● Here, on execution, the .write() method returns the number of
characters written into the file.
File operation
● Writing/Creating a File
● Another file mode used for writing to the file is the ‘a’ append
mode. This creates a new file if the specified file does not exist in
the current working directory. Unlike ‘w’ mode, it does not
remove the existing content from the file. Instead, it appends or
adds the new content at the end of the file. Writing in append
mode can be done using the .write() method.
● Example:
File operation
● Closing a File in Python
● At last, after opening and performing the reading, writing
operations, it is important to close the file. This is done using
.close() method. Let’s understand this with an example:
File operation
● Truncating a File
● Truncating a File is also possible in Python. By using the
.truncate() method, we can truncate the file up to the desired
length. Let’s understand this with an example:
File operation
● Renaming a File
● Renaming a file in Python can be done using the os Module and
needed to be imported to perform such tasks. The os module in
Python has a vast collection of methods to perform all the
essential file management tasks in Python itself.
File operation
● Deleting a File
● We can remove the files from a directory using Python’s versatile
os module. The os module has a method .remove() which
performs the task. The .remove() method takes a single
argument as string type which is our file name. Let’s understated
this with an example.
www.paruluniversity.ac.in

You might also like