I am very new to programming, and have hit a wall with file handling this week. I have the following prompt:
Fixed Length Records
You will be passed the filename P, firstname F, lastname L, and a new birthday B.
Load the fixed length record file in P, search for F,L in the first and change birthday to B.
Then save the file.
The inputs are provided:
import sys
P= sys.argv[1]
F= sys.argv[2]
L= sys.argv[3]
B= sys.argv[4]
My Code:
file1 = open(P, 'r')
data = file1.read()
for i in range(0, len(data)):
if F and L not in data:
data.append(i)
if F and L in data:
data.replace(B,B)
file1 = open(P, 'w')
file1.write(data)
file1.close()
This is obviously not working, but I am spinning wheels. I need to figure out how to change B, but can't quite get there. This is the output:
Program Output
Your program output did not match the expected output.
Your output:
Adam Smith 11111985Theodore Anderson
03201990Monty Biscuit-Barrel 10181980Adam Smithers
10101960Ruthy Anderson 06062010Expected output:
Adam Smith 11111985Theodore Anderson
03201990Monty Biscuit-Barrel 10181980Adam Smithers
00000000Ruthy Anderson 06062010
I can't figure out how to change the birthday to B.
It would help if we could see the arguments being passed to the program.
Also, in your loop, when you are testing for:
if F and L not in dataYou're actually saying:
if F != None and L not in dataTry this instead:
if F not in data and L not in data