0% found this document useful (0 votes)
33 views66 pages

File Handling 2 1

Uploaded by

Amita Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views66 pages

File Handling 2 1

Uploaded by

Amita Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 66

File handling -2

 Python has in-built functions to create, read, write, and manipulate


accessible files. The io module is the default module for accessing files
that can be used off the shelf without even importing it.
 Before you read, write, or manipulate the file, you need to make use of
the module open(filename, access_mode) that returns a file object called
"handle".
 Filesin Python are also treated as an object, which has its own attributes
and methods.
 text files have an End-Of-Line (EOL) character to indicate
each line's termination. In Python, the new line character (\
n) is the default EOL terminator.
 Since binary files store data after converting it into a binary
language (0s and 1s), there is no EOL character. This file
type returns bytes. This file is used when dealing with non-
text files such as images, .exe, or .pyc
Open()
 The built-in Python function open() has the following
arguments:
open(file, mode='r', buffering=-1, encoding=None,
errors=None, newline=None, closefd=True, opener=None)
 The open() function has almost 8 parameters along with
their default values for each argument as shown above
Make sure file name and path given is correct,
otherwise you'll get a FileNotFoundError:
Write()

Note: write() method doesn't write data to a file, but


to a buffer, it does, but only when the close() method
is called.
This latter method flushes the buffer and writes the
content to the file. If you wish not to close the file
using fileObject.flush() method to clear the buffer and
write back to file.
EXCEPTION HANDLING

try:
my_file_handle=open("folder/test.txt")
except IOError:
print("File not found or path is incorrect")
finally:
print("exit")
Access Modes

read-only mode
write-only mode
append mode
both read and write mode
Reading from a text file

 read([n]) : reads entire file


 readline([n]) : reads file line by line. It does not read more than one
line.
 readlines() : reads all lines and returns a list of all lines

* n indicates the number of bytes to be read, if n is not specified then


complete file is read.
Difference between readline and readlines
method on python
readline() method will read and return line from
the file.
readlines() method will read and return all the
lines in a file in the format of a list where each
element is a line in the file
Pickle module

Pickling refers to the process of converting


structured data into stream of bytes before writing
in a file.

import pickle
pickle.dump() to write on a file

 import pickle
 L1=[1,2,3,4,5]
 F=open(‘d:\\filelist.dat’,wb)
 pickle.dump(L1,F)
 print ( “list written on file”)
 F.close()
pickle.load () to read from a file
import pickle
list1 = {'m':12,'n':23,'t':34,'s':45,'u':56}
f1= open("d:\\mfile2.txt", 'wb')
pickle.dump(list1,f1) #writing content on the file
print ( "written")
f1.close()
print ( '***Reading from a file***')
f2= open ("d:\\mfile2.txt",'rb')
m1=pickle.load(f2) // print (pickle.load(f2))
print (m1)
print ( '****************')
f2.close()
output

RESTART: C:\Users\admin\AppData\Local\Programs\Python\Python37-32\
readwritedicttxt.py
written
***Reading from a file***
{'m': 12, 'n': 23, 't': 34, 's': 45, 'u': 56}
****************
import pickle
f1= open("d:\\mfile3.txt", 'wb')
for i in range(0,10):
x=int(input('enter an integer:'))
pickle.dump(x,f1) #writing content on the file

f1.close()
Content in the binary mode
For loop for read file line by line
Write( ) function

write(string) (for text) or write(byte_string)


(for binary)
writelines(list)
seek ( ) and tell( ) method

End-relative seeks such as seek(-2,2) are not allowed if


file mode does not include 'b', which indicates binary
format.
Only forward operations such as seek(0,2) are allowed
when the file object is dealt with as a text file.
CSV file - Comma Separated Values file
 Comma Separated Values
(CSV) files, which contain data Delimited files, which contain data
values that are separated by , values with a user-specified delimiter.
This can be a \t tab or a symbol (#,&,||),
for example: for example:
NAME,ADDRESS,EMAIL NAME||ADDRESS||EMAIL
ABC||CITY A||[email protected]
ABC,CITY A,[email protected] LMN||CITY B||[email protected]
PQR||CITY C||[email protected]
LMN,CITY B,[email protected]
PQR,CITY C,[email protected]
Import CSV module

 Python allows you to open text files and read the content either all at
once, or line-by-line.
 CSV is a module built-in to Python which will simplify working with
CSV files.
 We need to import CSV module to work with csv files.

import csv
Opening a csv file:
<file object> = open(<name of csv file>)
# Write data on a file
fin=open("D:\\firstfile.txt",'r')
f2=open("d:\\secondfile.txt",'w')
s=fin.read(str)
f2.write(s)
print(s)
fin.close( )
Reader a csv file
csv.reader()
Reader function in csv module is used to read the contents of the files. content in the file are parsed and read as one
line at a time.
The line in parsed as a list and each line is treated as a list element. So, it may be thought of this way that each the
object in which data is read is a list of lists.
For example :

C=csv.reader(<file object>)
Str=f.read()
In above example, c is an object that is a list of lists.
 To access the content from the object containing the data read from
csv file, a for loop should be used as follows :

for r in c :
print (r)

Every row that we are looping over is a list of lists


Reading a CSV file

import csv

f = open('E:\\TLIST.csv')
csv_f = csv.reader(f)
for row in csv_f:
print (row)
Reading CSV file and displaying only 1st and
2nd column of the row
import csv

f = open('E:\\TLIST.csv')
csv_f = csv.reader(f)
for row in csv_f:
print (row[1], "\t",row[2])
Displaying only 10 records at a time
import csv
f = open('E:\\TLIST.csv')
csv_f = csv.reader(f)
c=0
for row in csv_f:
print (row[1], "\t",row[2])
c=c+1
if c==10:
x=input()
c=0
Reading a text file using a csv reader function
import csv

f = open('E:\\results\\650482016.txt')
csv_f = csv.reader(f)

c=0
for row in csv_f:
print (row)
Creating separate lists from the data read
 For example : let us assume that a file has user type, name and its email address.

import csv
f = open('E:\\tlist.csv') The example
csv_f = csv.reader(f)
namelist=[]
creates a list
for r in csv_f: having names of
namelist.append(r[1])
teachers
print (namelist)
writer( ) : Writing in a CSV File .
Converting a list into a set helps in comparing
two lists
 Set is a datatype that contains unique values :
import csv for row in csv_f:
f = open('attendees1.csv') attendee_emails2.append(row[2])
csv_f = csv.reader(f)
attendee_emails1 = [] attendee_emails1 = set(attendee_emails1)
for row in csv_f: attendee_emails2 = set(attendee_emails2)
attendee_emails1.append(row[2])
f = open('attendees2.csv')
csv_f = csv.reader(f)
attendee_emails2 = []
Finding the difference between the 2 sets
 https://www.protechtraining.com/training/search?q=python
Connecting sql with python

Python can be used in database applications.


One of the most popular databases is MySQL.
Install MySQL Driver

 Python needs a MySQL driver to access the MySQL database.


 In this tutorial we will use the driver "MySQL Connector".
 We recommend that you use PIP to install "MySQL Connector".
 PIP is most likely already installed in your Python environment.
 Navigate your command line to the location of PIP, and type the
following:
Download and install "MySQL Connector":

C:\Users\admin\AppData\Local\Programs\
Python\Python37-32\Scripts>python -m pip
install mysql-connector-python
Download and installation of MySQL driver
complete
Test installation of my sql connector

Use import command to import sql connector as


follows :
>>> import mysql.connector
Create Connection

import mysql.connector
create an object of database for connectivity
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword“ )
print(mydb)
Output :
>>>
RESTART: C:/Users/admin/AppData/Local/Programs/Python/Python37-32/sqltablaeempl.py
<mysql.connector.connection.MySQLConnection object at 0x024B02D0>
>>>
querying the database using SQL statements

Create a cursor : cursor is basically a buffer to store


commands and values.
Syntax:
 <cur_object>= <databaseobject.>.cursor()
Command to execute:
 <cursorname>.execute(“<sqlcommand>”)
Create Connection
import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword“
database="mydatabase"
)
print(mydb)
import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase“ )

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"


val = ("John", "Highway 21")
mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record inserted.")


 import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = [ ('Peter', 'Lowstreet 4'), ('Amy', 'Apple st 652'),
('Hannah', 'Mountain 21'), ('Michael', 'Valley 345'),
('Sandy', 'Ocean blvd 2'), ('Betty', 'Green Grass 1'),
('Richard', 'Sky st 331'), ('Susan', 'One way 98'),
('Vicky', 'Yellow Garden 2'), ('Ben', 'Park Lane 38'),
('William', 'Central st 954'), ('Chuck', 'Main Road 989'),
('Viola', 'Sideway 1633')
]
mycursor.executemany(sql, val)
mydb.commit()
print(mycursor.rowcount, "was inserted.")
 Insert one row, and return the ID:
 import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"


val = ("Michelle", "Blue Village")
mycursor.execute(sql, val)

mydb.commit()

print("1 record inserted, ID:", mycursor.lastrowid)


 Selecting Columns
 To select only some of the columns in a table, use the "SELECT" statement followed by the column name(s):
 Example
 Select only the name and address columns:
 import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT name, address FROM customers")

myresult = mycursor.fetchall()

for x in myresult:
print(x)
Using the fetchone() Method
If you are only interested in one row, you can use the fetchone() method.
The fetchone() method will return the first row of the result:

Example
Fetch only one row:
import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchone()

print(myresult)
Select With a Filter
When selecting records from a table, you can
filter the selection by using the "WHERE"
statement:
 Select record(s) where the address is "Park Lane 38": result:
 import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

sql = "SELECT * FROM customers WHERE address ='Park Lane 38'"

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
print(x)
Wildcard Characters
You can also select the records that starts, includes, or ends with a given letter or phrase.
Use the % to represent wildcard characters:

Select records where the address contains the word "way":

import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

sql = "SELECT * FROM customers WHERE address LIKE '%way%'"

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
print(x)
Escape query values by using the placholder
%s method:
 import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

sql = "SELECT * FROM customers WHERE address = %s"


adr = ("Yellow Garden 2", )

mycursor.execute(sql, adr)

myresult = mycursor.fetchall()

for x in myresult:
print(x)
 Sort the Result
 Use the ORDER BY statement to sort the result in ascending or descending order.
 The ORDER BY keyword sorts the result ascending by default. To sort the result in descending order, use the
DESC keyword.
 import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

sql = "SELECT * FROM customers ORDER BY name"

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
print(x)
ORDER BY DESC

 import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

sql = "SELECT * FROM customers ORDER BY name DESC"

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
print(x)
Python MySQL Delete From By
 import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

sql = "DELETE FROM customers WHERE address = 'Mountain 21'"

mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, "record(s) deleted")


 Prevent SQL Injection
 It is considered a good practice to escape the values of any query, also in delete statements.

 This is to prevent SQL injections, which is a common web hacking technique to destroy or misuse your database.

 The mysql.connector module uses the placeholder %s to escape values in the delete statement:
Deleting records
 import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

sql = "DELETE FROM customers WHERE address = %s"


adr = ("Yellow Garden 2", )

mycursor.execute(sql, adr)

mydb.commit()

print(mycursor.rowcount, "record(s) deleted")


Python MySQL Drop Table
 Delete the table "customers":
 import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

sql = "DROP TABLE customers"

mycursor.execute(sql)
Drop Only if Exist
 import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

sql = "DROP TABLE IF EXISTS customers"

mycursor.execute(sql)
Python MySQL Update Table
 import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Valley 345'"

mycursor.execute(sql)

mydb.commit()
Python MySQL Limit
 import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM customers LIMIT 5")

myresult = mycursor.fetchall()

for x in myresult:
print(x)
Start From Another Position : If you want to return five records,
starting from the third record, you can use the "OFFSET"
keyword:
 import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM customers LIMIT 5 OFFSET 2")

myresult = mycursor.fetchall()

for x in myresult:
print(x)


Python MySQL Join
Join Two or More Tables
You can combine rows from two or more tables, based on a related column between them, by using a JOIN
statement.
 Consider you have a "users" table and a "products" table:
 users
 { id: 1, name: 'John', fav: 154},
{ id: 2, name: 'Peter', fav: 154},
These two tables can be
{ id: 3, name: 'Amy', fav: 155}, combined by using
{ id: 4, name: 'Hannah', fav:},
{ id: 5, name: 'Michael', fav:}
users' fav field and
 products products' id field.
 { id: 154, name: 'Chocolate Heaven' },
{ id: 155, name: 'Tasty Lemons' },
{ id: 156, name: 'Vanilla Dreams' }
Join users and products to see the name of the users favorite
product:
 import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
INNER JOIN products ON users.fav = products.id"

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
LEFT JOIN : In the example above, Hannah, and Michael were excluded from the result, that is
because INNER JOIN only shows the records where there is a match.
If you want to show all users, even if they do not have a favorite product, use the LEFT JOIN
statement:

 sql = "SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
LEFT JOIN products ON users.fav = products.id"
RIGHT JOIN
If you want to return all products, and the users who have them as their
favorite, even if no user have them as their favorite, use the RIGHT JOIN
statement:
 sql= "SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
RIGHT JOIN products ON users.fav = products.id"

You might also like