def
copyFile(oldFile, newFile):
f1 =
open(oldFile, "r")
f2 =
open(newFile, "w")
while
True:
text =
f1.read(50)
if text
== "":
break
f2.write(text)
f1.close()
f2.close()
return
def
exists(filename):
try:
f =
open(filename)
f.close()
return
True
except
IOError:
return
False
try:
x=
float(raw_input("Your number: "))
inverse =
1.0 / x
except
ValueError:
print
"You should have given either an int or a float"
except
ZeroDivisionError:
print
"Infinity"
cl=0
cw=0
cc=0
f=open("ex88.txt","r")
for line in f:
words=line.split()
cl +=1
cw
+=len(words)
cc+=len(line)
print('No.
of lines:',cl)
print('No.
of words:',cw)
print('No.
of characters:',cc)
f.close()
import
sys
def
inputCmd():
print
("Name of the script:", sys.argv[0])
print
("Number of arguments:", len(sys.argv))
print
("The arguments are:", str(sys.argv))
5. Mention the commands and their
syntax for the following: get current directory, changing directory, list,
directories and files, make a new directory, renaming and removing directory.
(a) Get
current directory: getcwd()
Syntax :
import os
os.getcwd()
(b) Changing
directory: chdir()
Syntax:
os.chdir(‘C:\\Users’)
os.getcwd()
(c) List
directories and files: listdir()
Syntax:
os.listdir()
(d) Making
a new directory: mkdir()
Syntax:
os.mkdir(‘Newdir’)
(e) Renaming
a directory: rename()
os.rename(‘Newdir’,’Newname’)
os.listdir()
(f) Removing
a directory: remove()
os.remove(‘NewName’)
def
getStack():
return[]
def
isempty(s):
if s==[]:
return
True
else:
return
False
def
top(s):
if
isempty(s):
return
None
else:
return
s[len(s)-1]
def
push(s,item):
s.append(item)
def
pop(s):
if
isempty(s):
return
None
else:
item=s[len(s)-1]
del
s[len(s)-1]
return
item
import
stack
def
today():
mystack=stack.getStack()
for item
in range(1,7):
stack.push(mystack,item)
print('Pushing',item,'to
stack')
print
('Stack items')
while not
stack.isempty(mystack):
item=stack.pop(mystack)
print('Poping',item,'from
stack')
def
test():
wordcount.wordCount()
def
test2():
ex12.inputNumber()
def
test3():
ex97.fun()
ex97.py:
def
fun():
try:
x=
float(raw_input("Your number: "))
inverse =
1.0 / x
except
ValueError:
print
"You should have given either an int or a float"
except
ZeroDivisionError:
print
"Infinity"
ex12.py:
def inputNumber
() :
x = input
('Pick a number: ')
if x ==
17 :
raise
ValueError, '17 is a bad number'
return x
wordcount.py:
def
wordCount():
cl=0
cw=0
cc=0
f=open("ex88.txt","r")
for line
in f:
words=line.split()
cl +=1
cw
+=len(words)
cc+=len(line)
print('No.
of lines:',cl)
print('No.
of words:',cw)
print('No.
of characters:',cc)
f.close()
def
funMap():
cities =
["Chennai", "delhi","Mumbai","Kolkata"]
fh=open("cities.pck","w")
pickle.dump(cities,fh)
fh.close()
f=open("cities.pck","r")
cts=pickle.load(f)
print(cts)
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.