I'm teaching myself programming and today's challenge is to write a program that can align text left,right or centre. My major problem is that once I open a file, I'm not sure how to write to specific lines. Is there an easy way to do this? This is what I have. I'm thinking I will need to somehow figure the bytes in the line so that I can f.seek to the end of the line. I feel like python would already have this type of functionality somewhere but I couldn't find it searching online. Any suggestions?
def align():
name= input('what is the name of your txt file?: ')
try:
f=open(name + '.txt','r+')
lines =f.readlines()
just = input('left, right, or centre?: ')
for i in range[,lines]:
j = lines[i].strip('\n')
if just == 'right':
f.seek() #I want it to seek each line but can't figure out what variable might go here...
f.write('{:>30}'.format(line.strip()))
elif just == 'left':
f.seek() #I want it to seek each line but can't figure out what variable might go here...
f.write('{:<30}'.format(line.strip()))
f.seek() #I want it to seek each line but can't figure out what variable might go here...
elif just == 'centre' or just == 'center':
f.write('{:^30}'.format(line.strip()))
else:
print("You didn't choose a justification!")
f.close()