UNIT 4 Dr. JPSM PPT - Python
UNIT 4 Dr. JPSM PPT - Python
UNIT 4 Dr. JPSM PPT - Python
2. Binary Files
• Binary files are also stored in terms of bytes (0s and 1s),
but unlike text files, these bytes do not represent the ASCII
values of characters.
Continue..
• They represent the actual content such as image, audio,
video, compressed versions of other files, executable
files, etc.
• We need specific software to read or write the contents
of a binary file.
• These files are not human readable. Thus, trying to open
a binary file using a text editor will show some garbage
values.
• We can read and write both text and binary files through
Python programs.
OPENING AND CLOSING A TEXT 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.
ab+ Opens a file for both appending and reading in binary format.
Example
The open command opens the file in reading mode and prints each line of
the file with for loop.
1. writelines() method
• The file object also has writelines() method to write
items in a list object to a file. The newline characters ("\
n) should be the part of the string.
Example
• f=open("python.txt","w")
• f.writelines(lines)
• f.close()
2. readline() method
• This method reads current line till it encounters newline
character.
Continue…
Example
• f=open('python.txt','r')
• f.readline()
• f=open("python.txt","r")
• while True:
• line=f.readline()
• if line=='':break
• print (line)
• f.close()
See code
Working with Directories
• Directories are a way of storing, organizing, and separating
the files on a computer.
• Python contains several modules that has a number of built-
in functions to manipulate and process data.
• Every process in the computer system will have a directory
associated with it, which is known as Current Working
Directory(CWD). os.chdir() method is used to change it.
• Example
import filecmp as fc
import os
dir_1 = dir_2 = os.getcwd()
# creating object and invoking constructor
d = fc.dircmp(dir_1, dir_2, ignore=None, hide=None)
print("comparison 1 :")
d.report()
3. tempfile
• This module is used to create temporary files and
directories.
• This creates the temporary files and directories in the
temp directories created by the Operating systems. For
windows, the temp files can be found at the following path:
profile/AppData/Local/temp
• mkdtemp() :
• It is used to create a temporary directory by passing the
parameters suffix, prefix and dir.
• Example
import tempfile as tf
f = tf.mkdtemp(suffix='', prefix='tmp')
print(f)
4. shutil
• This module is concerned with number of high-level
operations on files and directories. It allows
copying/moving directories from a source to destination.
• Syntax 3 shutil.move(s,d):
Practice set
1. Write a Python program to list only directories,
files and all directories, files in a specified path.
2. Write a Python program to get the size,
permissions, owner, device, created, last
modified and last accessed date and time of a
specified path.
3. Write a Python program to find the parent's
process ID, real user ID of the current process
and change the real user ID.
4. Write a Python program to run an operating
system command using the OS module.
Database Programming
• A database is nothing but an organized collection of data.
Data is organized into rows, columns and tables and it is
indexed to make it easier to find relevant information.
import mysql.connector
mydb=mysql.connector.connect(host="localhost",
user="root",passwd="root" ,database=“College")
mycursor=mydb.cursor()
mycursor.execute("create table student(rollno
int(3) primary key,name varchar(20),age int(2))")
How to change table structure/(add,edit,remove
colum of a table) at run time