I want to append spaces at the end of line if line does not have 80 characters in Python. I have referred How can I fill out a Python string with spaces? from StackOverflow. Please find input and output as below. Could you please advise me to change any code? Thank you.
What I have tried?
I have written the following code:
#!/usr/bin/env python
import sys
input_file = open(sys.argv[1],'r')
lines = input_file.readlines()
sys.stdout = open(sys.argv[2],'w')
for line in lines:
if len(line) != 80:
print(line.ljust(80, ' '))
else:
print(line[0:80])
sys.stdout.close()
input_file.txt
01010001ADPBI001PEACH ADP6619200508215754
01010002SCOBIADPADPPW06 200508215754 ADP4
01065008200511P6206 T YS2020050720200511COMO 4005181997 BLTF0510002
expected_output_file.txt
01010001ADPBI001PEACH ADP6619200508215754
01010002SCOBIADPADPPW06 200508215754 ADP4
01065008200511P6206 T YS2020050720200511COMO 4005181997 BLTF0510002
As per above program, I am getting the below output:
01010001ADPBI001PEACH ADP6619200508215754
01010002SCOBIADPADPPW06 200508215754 ADP4
01065008200511P6206 T YS2020050720200511COMO 4005181997 BLTF0510002