outfile = open("prodfile1.txt","w")
cProd = raw_input("Enter product, (only Enter to exit:")
while (len(cProd) > 0):
outfile.writelines(cProd)
outfile.writelines("\n")
cProd = raw_input("Enter product, (only Enter to exit:")
outfile.close()
infile = open("prodfile1.txt","r")
while (1):
cProd = infile.readline()
if(cProd):
print cProd
else:
break
infile.close()
cProd = raw_input("Enter new product ")
outfile = open("prodfile.txt1","a")
outfile.writelines(cProd)
outfile.close()
iProdId = 1
outfile = open("prodfile.txt","w")
while (iProdId != 0):
iProdId = int(raw_input("Enter product id: "))
dPrice = float(raw_input("...and price: "))
if(iProdId > 0):
outfile.writelines(str(iProdId) + '\n' + str(dPrice) + '\n')
outfile.close()
iFound = 0
infile = open("prodfile.txt","r")
iSrch = int(raw_input("Enter product id: "))
while(1):
iProdId = infile.readline()
if(iProdId == ""):
break
iProdId = int(iProdId)
dPrice = float(infile.readline())
if(iProdId == iSrch):
print "The price is:" , dPrice
iFound = 1
break
if(not iFound):
print "Product missing"
infile.close()
def sort(cList,n):
for v in range(0,n-1):
for h in range(v+1,n):
if(cList[h]<cList[v]):
temp = cList[v]
cList[v] = cList[h]
cList[h] = temp
i = 0
infile = open("prodfile1.txt","r")
cProd = infile.readlines()
infile.close()
while(i < len(cProd)):
i = i + 1
iNo = i
sort(cProd,iNo)
for j in range(0,iNo):
print cProd[j]
import os
infile = open("prodfile.txt","r")
outfile = open("temp.txt","w")
iSrch = int(raw_input("Specify product id: "))
while(1):
iProdId = infile.readline()
if(iProdId == ""):
break
iProdId = int(iProdId)
dPrice = float(infile.readline())
if(iProdId == iSrch):
dPrice = float(raw_input("Specify the new price: "))
outfile.writelines(str(iProdId) + '\n' + str(dPrice) + '\n')
infile.close()
outfile.close()
os.remove("prodfile.txt")
os.rename("temp.txt","prodfile.txt")
import os
cNewName = raw_input("Specify new file name: ")
infile = open("prodfile.txt","r")
outfile = open(cNewName,"w")
if(not os.path.exists(cNewName)):
print "The file could not be created"
outfile.writelines(infile.readlines())
infile.close()
outfile.close()