fp = open ("C:/Users/Akshatha M/Desktop/carrot.txt", "r" ) #open file in read mode
while ( 1 ):
ch = fp.read(1)
if not ch :
break
print ch
fp.close() #close file
try :
fp = open ("C:/Users/Akshatha M/Desktop/carrot.txt", "r" )#open file in read mode and check if file is opened successfully
except:
print "cannot open file"
exit()
#Variable declaration
nol = 0
Not = 0
nob = 0
noc = 0
fp = open ("C:/Users/Akshatha M/Desktop/carrot.txt", "r" ) #open file in read mode
while ( 1 ):
ch = fp.read(1)
if not ch :
break
noc = noc + 1 #number of chars
if ( ch == " " ):
nob = nob + 1 #number of spaces
if ( ch == "\n" ):
nol = nol + 1 # number of newlines
if ( ch == "\t" ):
Not = Not + 1 #number of tabs
fp.close() #close file
#Result
print "Number of characters = ", noc
print "Number of blanks = ", nob
print "Number of tabs = ", Not
print "Number of lines = ", nol
fs = open ("C:/Users/Akshatha M/Desktop/carrot.txt", "r" ) #open file in read mode
if(not fs):
print "Cannot open source file"
exit()
ft = open ( "C:/Users/Akshatha M/Desktop/temp.txt", "w" ) #open file in write mode
if(not ft):
print "Cannot open target file"
fs.close()
exit()
while ( 1 ):
ch = fs.read(1)
if not ch :
break
else:
ft.writelines(ch) #write into target file
#closen files
fs.close()
ft.close()
fp = open ( "C:/Users/Akshatha M/Desktop/temp.txt", "w" ) #open file in write mode
if(not fp):
print "Cannot open target file"
exit()
print "Enter a few lines of text:"
#s=input("") #Input strings from keyboard
s = "File written"
print s
while ( len(s) > 0 ):
fp.writelines(s) #write into file
fp.writelines("\n")
#s=input("") #Input strings from keyboard
s=""
#close files
fp.close()
fp = open ("C:/Users/Akshatha M/Desktop/carrot.txt", "r" ) #open file in read mode
if ( not fp ):
print "Cannot open file"
exit( )
while ( 1 ) :#Read strings from file
s = fp.read(79)
if(s):
print s
else:
break
fp.close() #close file
from collections import namedtuple
#Variable declaration
another = 'Y'
#Structure defintion
struct_emp = namedtuple("struct_emp", "name age bs")
fp = open ( "C:/Users/Akshatha M/Desktop/Employee.dat", "w" ) #open file in write mode
if(not fp):
print "Cannot open target file"
exit()
while ( another == 'Y' ):
print "Enter name, age and basic salary: "
#en,ea,ebs = input("").split()
en ="John"
ea="34"
ebs="25000"
print en,ea,ebs
e = struct_emp(en,ea,ebs)
fp.writelines(e) #write into file
fp.writelines("\n")
#another = input( "Add another record (Y/N): " )
print "Add another record (Y/N): "
another = 'N'
print another
#close file
fp.close()
from collections import namedtuple
#Structure defintion
struct_emp = namedtuple("struct_emp", "name age bs")
fp = open ( "C:/Users/Akshatha M/Desktop/Employee.txt", "r" ) #open file in read mode
if(not fp):
print "Cannot open target file"
exit()
while ( 1 ):
s = fp.readline()
if(s):
en,ea,ebs = s.split()
e = struct_emp(en,ea,ebs) #Read record
print e.name, e.age, e.bs
else:
break
#close file
fp.close()
fs = open ("C:/Users/Akshatha M/Desktop/Project1.exe", "rb" ) #open file in read binary mode
if(not fs):
print "Cannot open source file"
exit()
ft = open ( "C:/Users/Akshatha M/Desktop/NewProject1.exe", "wb" ) #open file in write binary mode
if(not ft):
print "Cannot open target file"
fs.close()
exit()
while ( 1 ):
ch = fs.read(1)
if not ch :
break
else:
ft.write(ch) #write into target file
#closen files
fs.close()
ft.close()
from collections import namedtuple
#Variable declaration
another = 'Y'
#Structure defintion
struct_emp = namedtuple("struct_emp", "name age bs")
fp = open ( "C:/Users/Akshatha M/Desktop/Employee.dat", "wb" ) #open file in write mode
if(not fp):
print "Cannot open target file"
exit()
while ( another == 'Y' ):
print ( "Enter name, age and basic salary: " )
#en,ea,ebs = input("").split()
en ="John"
ea="34"
ebs="25000"
print en,ea,ebs
e = struct_emp(en,ea,ebs)
#write into file
fp.write(b'e.name')
fp.write(b'e.age')
fp.write(b'e.bs')
fp.write(b'\n')
#another = input( "Add another record (Y/N): " )
print "Add another record (Y/N): "
another = 'N'
print another
#close file
fp.close()
from collections import namedtuple
#Structure defintion
struct_emp = namedtuple("struct_emp", "name age bs")
fp = open ( "C:/Users/Akshatha M/Desktop/Employee.txt", "rb" ) #open file in read mode
if(not fp):
print "Cannot open target file"
exit()
while ( 1 ):
s = fp.readline()
if(s):
en,ea,ebs = s.split()
e = struct_emp(en,ea,ebs) #Read record
print e.name, e.age, e.bs
else:
break
#close file
fp.close()
import os
from collections import namedtuple
#Structure defintion
struct_emp = namedtuple("struct_emp", "name age bs")
fp = open ( "C:/Users/Akshatha M/Desktop/Employee.txt", "r+b")
if ( not fp ):
fp = open ( "C:/Users/Akshatha M/Desktop/Employee.txt", "w+b" )
if ( not fp ):
print "Cannot open file"
exit( )
while ( 1 ):
print " 1. Add Records"
print " 2. List Records"
print " 3. Modify Records"
print " 4. Delete Records"
print " 0. Exit"
#choice = input("\nYour choice")
print "\nYour choice"
choice = 1
if(choice == '1'):
another = 'Y'
while ( another == 'Y' ):
print ( "Enter name, age and basic salary: " )
en,ea,ebs = input("").split()
e = struct_emp(en,ea,ebs)
#write into file
fp.write(b'e.name')
fp.write(b' ')
fp.write(b'e.age')
fp.write(b' ')
fp.write(b'e.bs')
fp.write(b'\n')
another = input( "Add another record (Y/N): " )
elif(choice == '2'):
fp.seek(0,0)
while ( 1 ):
s = fp.readline()
if(s):
en,j1,ea,j2,ebs = s.split()
e = struct_emp(en,ea,ebs) #Read record
print ( e.name, e.age, e.bs )
else:
break
elif(choice == '3'):
another = 'Y'
while ( another == 'Y' ):
empname =input("Enter name of employee to modify " )
fp.seek(0,0)
while (fp.readline()):
if ( b'empname' == b'e.name' ):
en,ea,ebs = input("Enter new name, age & bs" ).spilt()
e = struct_emp(en,ea,ebs)
cur = fp.tell()
fp.write(b'e.name')
fp.write(b'e.age')
fp.write(b'e.bs')
fp.write(b'\n')
break
print ( "\nModify another Record (Y/N) " )
another = input("")
elif(choice == '4'):
another = 'Y'
while ( another == 'Y' ):
empname = input("Enter name of employee to delete " )
ft = open ( "C:/Users/Akshatha M/Desktop/temp.txt", "wb" )
fp.seek(0,0)
while ( 1 ):
s = fp.readline()
if(s):
if ( not(b'empname' == b'e.name' )):
ft.write(s)
else:
break
fp.close()
ft.close()
os.remove("C:/Users/Akshatha M/Desktop/Employee.txt")
os.rename ( "C:/Users/Akshatha M/Desktop/temp.txt", "C:/Users/Akshatha M/Desktop/Employee.txt" )
fp = open ( "C:/Users/Akshatha M/Desktop/Employee.txt", "r+b" )
print ( "Delete another Record (Y/N) " )
another = input("")
else:
fp.close()#close file
exit( )
#source=input( "Enter source file name " )
print "Enter source file name "
source = "Employee.txt"
print source
source="C:/Users/Akshatha M/Desktop/"+source
inhandle = open (source, "rb" ) #open file in read binary mode
if(not inhandle):
print "Cannot open file"
exit()
#target=input( "Enter target file name " )
print "Enter target file name "
target = "temp.txt"
print target
target="C:/Users/Akshatha M/Desktop/"+target
outhandle = open ( target, "wb" ) #open file in write binary mode
if(not outhandle):
print "Cannot open target file"
inhandle.close()
exit()
while ( 1 ):
Bytes = inhandle.read(1)
if not Bytes :
break
else:
outhandle.write(Bytes) #write into target file
#closen files
inhandle.close()
outhandle.close()
fp = open ( "C:/Users/Akshatha/Documents/extrastuff/skills.txt", "w" )
while (1):
try:
ch = fp.read(1) #read character from file
except: #ferror()
print "Error in reading file"
break
print ch
fp.close()
import io
try:
fp = open ("C:/Users/Akshatha/Documents/extrastuff/skills.txt", "r")
except:
print "cannot open file"
exit()
io.open('stdprn','w')
try:
stdprn = io.open('stdprn', 'w') #open printer
while ( 1 ):
ch = fp.read(1)
if not ch :
break
else:
stdprn.write(ch) #write into printer
except: #no printer
#closen files
fp.close()
stdprn.close()