Chapter 7 : Files

Example 7.1, Page No 157

In [5]:
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()
Enter product, (only Enter to exit:wheel
Enter product, (only Enter to exit:handlebars
Enter product, (only Enter to exit:saddle
Enter product, (only Enter to exit:bike
Enter product, (only Enter to exit:moped
Enter product, (only Enter to exit:

Examle 7.2 Page No 159

In [2]:
infile = open("prodfile1.txt","r")
while (1):
    cProd = infile.readline()
    if(cProd):
        print cProd
    else:
        break
infile.close()
wheel

handlebars

saddle

bike

moped

Example 7.3, Page No 160

In [2]:
cProd = raw_input("Enter new product ")
outfile = open("prodfile.txt1","a")
outfile.writelines(cProd)
outfile.close()    
Enter new product Spoke

Example 7.4, Page No 161

In [3]:
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()
Enter product id: 2345
...and price: 245.5
Enter product id: 4512
...and price: 450.2
Enter product id: 3902
...and price: 320.7
Enter product id: 2478
...and price: 75.9
Enter product id: 0
...and price: 0

Example 7.5, Page No 163

In [2]:
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()
Enter product id: 1234
Product missing

Example 7.6, Page No 166

In [5]:
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]
Bike

Handlebars

Moped

Saddle

Spoke

Wheel

Example 7.7, Page No 169

In [9]:
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")
Specify product id: 2345
Specify the new price: 33.33

Example 7.8, Page No 171

In [3]:
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()
Specify new file name: tryal.txt
In [ ]: