File Handling (1)
File Handling (1)
File Handling (1)
File Implementation
Program:
fp=open("new_file2.txt","w+")
string='''hi welcome to School of computer Science engineering
VIT University, Chennai'''
fp.write(string)
print("File is ready to read...")
fp.seek(0,0) #why this line…
#fp.seek(12,0)
print(fp.read())
fp.close()
Knowing whether a file exists or not
OS module has a sub module by the name ‘path’ that contains a method
isfile()
isfile() – used to know about the status of a file
Os.path.isfile()-gives ‘True’ when file already exists else ‘False’
Syntax:
if os.path.isfile(file_name):
f= open(file_name, “r”)
Import the following modules
else:
import os, sys
print(file_name, “ does not exist”)
sys.exit()
Program to count total no of lines and print line by line from a file
for line in file_object:
print(line)
Program to count no of lines, words and character in a text file
import sys, os
name=input(“enter your file name”)
if os.path.isfile(name):
f=open(name, “r”)
i/p:
else:
print(name, “does not exit”) welcome.txt
sys.exit() Problem Solving Python and Programming
cl=cw=cc=0
o/p:
for line in f:
words=line.split() enter your file name welcome.txt
cl=cl+1 no. of lines 1
cw=cw+len(words)
no. of words 5
cc=len(line)
print(“no. of lines”, cl) no. of characters 38
print(“no. of words”,cw) >>>
print(“no. of characters”,cc”)
f.close()
Rename and deleting file
OS module provides methods to rename and delete files
Syntax:
os.rename(current_file_name, new_file_name)
os.remove(file_name) Import the following module
Example: import os
import os
os.rename(“welcome.txt”, “mech.txt”)
os.remove(“mech.txt”)
Command Line Arguments(1/2)
Python provides another method to give input, called command line
arguments
Diff: input() is used to get i/p while script is running where as command line
arg. is used to get i/p before script is running
purpose:
sys.argv #list of arguments
len(sys.argv) #number of command line arguments
Example: o/p:
import sys name of the script
print(“name of the script”, sys.argv[0]) c:/program/Appdata/Python/first.
py
print(“length of the script”, len(sys.argv))
length of the script 1
print(“arguments are”, sys.argv)
arguments are
[‘c:/program/Appdata/Python/fir
st.py’]
Command Line Arguments(2/2)
Variable arguments
args(*) and kwargs(**) are used to get arbitrary no of arguments in a
function
def command(*vals):
print(“values are:”, vals)
print(“type of vals”, type(vals)) o/p:
mul=1 values are: (5,6)
for i in vals:
type of vals: class ‘tuple’>
mul=mul*i
result is: 30
return mul
o/p:
res=command(5,6)
print(“result is:”, res) values are: (2,5,5)
res=command(2,5,5) type of vals: class ‘tuple’>
print(“result is:”, res) result is: 50
Variable arguments-cont.,
args(*) and kwargs(**) are used to get arbitrary no of arguments in a
function
def command(**vals):
print(“values are:”, vals)
print(“type of vals”, type(vals)) o/p:
print(“Name = ”,vals[‘Name’]) values are: {‘Name’: ‘ Karthikeyan N’,
print(“Department = ”,vals[‘dept’]) ‘rollno’ : ‘7228’, ‘dept’ : ‘CSE’}
command(Name=“Karthikeyan N”, type of vals: class ‘dict’>
rollno=“7228”, dept=“CSE”) Name = Karthikeyan N
Department = CSE
Python File - Exercises
1. Write a Python program to count the frequency of words in a file
Refer I_CSE_B.txt file as input file
2. Write a Python program to copy the contents of a file to another file
copy from I_CSE_B.txt to II_CSE_B.txt
3. Write a python program to read the contents from two file and merge into
another file
copy from I_CSE_B.txt to II_CSE_B.txt to III_CSE_B.txt
4. Write a python program to print the vowel letter of I_CSE_B.txt file
Thank You
Q&A