Python Add string to each line in a file

后端 未结 3 1109
野的像风
野的像风 2021-01-01 18:08

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()
3条回答
  •  猫巷女王i
    2021-01-01 18:41

    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')
    

提交回复
热议问题