This is my database named myDB.txt
:
28273139
iPhone
5.50
30
5
35
81413852
Book
1.50
43
10
55
The text file database follows the format
Product Code
Product Name
Price
Current Stock
Reorder Stock (the lowest the stock can go)
Target Stock
Now, the I want to update the iPhone data. This is the iPhone data.
28273139
iPhone
5.50
30
5
35
It uses the 8-digit product code to identify the product.
I have written the following code.
userCode = "28273139"
newstock = "29"
database = open('myDB.txt','r+')
while(str(userCode) not in next(database)):
pass
else:
for i in range(3):
line = next(database)
replace = line.replace(line,newstock)
database.write(replace)
I want to change the 4th value of the list, the number 30 to the number 29, which I declared as the variable newstock. The program moves 3 lines down, since the format is the same for every product in the database and replaces the line with this new value.
The result I need from updating the iPhone data -
28273139
iPhone
5.50
29
5
35
However, this method doesn't work.
I receive an error every time.
AttributeError: 'str' object has no attribute 'readlines'
Can someone help fix my problem? Is the code I have written correct to produce the required result?