fp=open('PR1.txt','r') #open the file for reading
ch=fp.readlines() #ch will store all content of file
print "%s" % (' '.join(ch)) #prints content of file
print "\n"
fp.close()
try:
fp=open('yoyo.txt','r')
for line in fin:
print line
fp.close()
except:
print 'cannot open file'
nol=0
nom=0
nob=0
noc=0
fp=open('H4CK3R.txt','r')
while True:
ch=fp.read(1)
if not ch:
break
noc+=1
if ch==' ':
nob+=1
if ch=='\n':
nol+=1
if ch=='\t':
nom+=1
fp.close()
print "\n"
fp.close()
print "Number of characters=%d\n" % (noc)
print "Number of blanks=%d\n" % (nob)
print "Number of tabs=%d\n" % (nom)
print "Number of lines=%d\n" % (nol)
import sys
try:
fs=open('H4CK3R.txt','r')
except:
print "Cannot open file"
sys.exit(1)
try:
ft=open('python.txt','w')
except:
print "Cannot open file"
fs.close()
sys.exit(2)
ch=fs.readlines()
ft.writelines(ch)
fs.close()
ft.close()
try:
fp=open('POEM.txt','w')
except:
print 'cannot open file'
print "\nEnter a few lines of text:\n"
s=' '
while (len(s)>0):
s=raw_input()
fp.writelines(s)
fp.writelines("\n")
fp.close()
try:
fp=open('POEM.txt','r')
except:
print 'cannot open file'
s=fp.read(99)
print "%s" % (s)
print "\n"
fp.close()
class emp():
def __init__(self,**kwds):
self.__dict__.update(kwds)
another='Y'
e=emp()
try:
fp=open('EMPLOYEE.txt','w')
except:
print 'cannot open file'
while another=='Y':
print "\nEnter name,age and basic salary:"
e.name=raw_input()
e.age=eval(raw_input())
e.bs=eval(raw_input())
ch="%s %d %f" % (e.name,e.age,e.bs)
fp.writelines(ch)
fp.writelines("\n")
print "Add another record(Y/N)"
another=raw_input()
fp.close()
class emp():
def __init__(self,**kwds):
self.__dict__.update(kwds)
e=emp()
try:
fp=open('EMPLOYEE.txt','r') # open the file for reading
except:
print 'cannot open file'
for line in fp: # iterate over each line
e.name, e.age, e.bs = line.split() # split it by whitespace
e.age = int(e.age) # convert age from string to int
e.bs = float(e.bs) # convert bs from string to float
print "%s %d %f\n" %(e.name, e.age, e.bs)
fp.close()
import sys #for exit()
try:
fs=open('H4CK3R.txt','rb') # open the file for reading
except:
print 'cannot open file'
sys.exit(1)
try:
ft=open('python.txt','wb') # open the file for writing
except:
print 'cannot open file'
fs.close()
sys.exit(2)
ch=fs.readlines()
ft.writelines(ch)
fs.close()
ft.close()
class emp():
def __init__(self,**kwds):
self.__dict__.update(kwds)
another='Y'
e=emp()
try:
fp=open('EMP.txt','wb') # open the file for reading
except:
print 'cannot open file'
while another=='Y':
print "\nEnter name,age and basic salary:"
e.name=raw_input()
e.age=eval(raw_input())
e.bs=eval(raw_input())
ch="%s %d %f" % (e.name,e.age,e.bs)
fp.writelines(ch)
print "Add another record (Y/N)"
another=raw_input()
if another=='Y':
fp.writelines("\n")
fp.close()
class emp():
def __init__(self,**kwds):
self.__dict__.update(kwds)
e=emp()
try:
fp=open('EMP.txt','rb')
except:
print "Cannot open file"
for line in fp:
e.name, e.age, e.bs = line.split() # split it by whitespace
e.age = int(e.age) # convert age from string to int
e.bs = float(e.bs) # convert bs from string to float
print "%s %d %f\n" %(e.name, e.age, e.bs)
fp.close()
class emp():
def __init__(self,**kwds):
self.__dict__.update(kwds)
e=emp()
try:
fp=open('EMP.txt','rb+') # open the file for reading
except:
try:
fp=open('EMP.txt','wb+') # open the file for writing
except:
print 'cannot open file'
while 1:
print "1.Add Records"
print "2.List Records"
print "3.Modify Records"
print "4.Delete Records"
print "0.Exit"
print "Your choice"
choice=eval(raw_input())
def add():
import os
fp.seek(0,os.SEEK_END)
another='Y'
while another=='Y':
print "\nEnter name,age and basic salary:"
e.name=raw_input()
e.age=eval(raw_input())
e.bs=eval(raw_input())
ch="%s %d %f\t\t\t\t\t\t\t\t\t\t" % (e.name,e.age,e.bs)
fp.writelines("\n")
fp.writelines(ch)
print "Add another record(Y/N)"
another=raw_input()
def list():
import os
fp.seek(0,os.SEEK_SET)
for line in fp: # iterate over each line
if len(line)>10:
e.name, e.age, e.bs = line.split() # split it by whitespace
e.age = int(e.age) # convert age from string to int
e.bs = float(e.bs) # convert bs from string to float
print "%s %d %f\t\t\t\t\t\t\t\t\t\t" %(e.name, e.age, e.bs)
def modify():
another='Y'
while another=='Y':
print "\nEnter name of employee to modify"
empname=raw_input()
import os
fp.seek(0,os.SEEK_SET)
for line in iter(fp.readline, ''):
if len(line)>10:
e.name, e.age, e.bs=line.split()
e.age = int(e.age) # convert age from string to int
e.bs = float(e.bs)
if(cmp(e.name,empname)==0):
c=len(line)
print "\nEnter new name,age & bs"
e.name=raw_input()
e.age=eval(raw_input())
e.bs=eval(raw_input())
import os
fp.seek(-c,os.SEEK_CUR)
ch="%s %d %f" % (e.name,e.age,e.bs)
fp.writelines("\n")
fp.writelines(ch)
fp.writelines("\n")
break
print "\nModify another Record(Y/N)"
another=raw_input()
def delete():
another='Y'
global fp
while another=='Y':
print "\nEnter name of employee to delete"
empname=raw_input()
ft=open('TEMP.txt','wb')
import os
fp.seek(0,os.SEEK_SET)
for line in fp:
if len(line)>10:
e.name, e.age, e.bs=line.split()
e.age = int(e.age) # convert age from string to int
e.bs = float(e.bs)
if(cmp(e.name,empname)!=0):
ch="%s %d %f\t\t\t\t\t\t\t\t\t\t" % (e.name,e.age,e.bs)
ft.writelines(ch)
ft.writelines("\n")
fp.close()
ft.close()
import os
os.remove("EMP.txt") # Delete file EMP.txt
os.rename( "TEMP.txt", "D:/EMP.txt" ) # Rename a file from TEMP.txt to EMP.txt
fp=open('EMP.txt','rb+')
print "Delete another record(Y/N)"
another=raw_input()
def exit():
import sys
fp.close()
sys.exit(0)
def switch(c):
return {1: add,
2: list,
3: modify,
4: delete,
0: exit,
}[c]()
switch(choice)
from StringIO import StringIO
buffer = StringIO()
print "\nEnter source file name"
source=raw_input()
try:
inhandle=open(source,'rb')
except:
print "Cannot open file"
print "Enter target file name"
target=raw_input()
try:
outhandle=open(target,'wb')
except:
print "Cannot open file"
inhandle.close()
bytes=inhandle.readlines()
buffer.write(bytes)
outhandle.writelines(buffer.getvalue())
buffer.close()
inhandle.close()
outhandle.close()