I need to open a text file and then add a string to the end of each line.
So far:
appendlist = open(sys.argv[1], "r").read()
Remember, using the +
operator to compose strings is slow. Join lists instead.
file_name = "testlorem"
string_to_add = "added"
with open(file_name, 'r') as f:
file_lines = [''.join([x.strip(), string_to_add, '\n']) for x in f.readlines()]
with open(file_name, 'w') as f:
f.writelines(file_lines)
s = '123'
with open('out', 'w') as out_file:
with open('in', 'r') as in_file:
for line in in_file:
out_file.write(line.rstrip('\n') + s + '\n')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With