String + Variable + String formatting in Python -


i'm new python please go easy, i'm sure question has simple solution, seems evading me. i'm trying write string file in format:

"string1" variable "string2" 

but seems write "string2" "string1" variable

here code:

inf = open("f:\test\users.txt", 'r') outf = open("f:\test\commands.txt", 'w')  line in iter(inf):     s = "{0} {1} {2}" .format("adduser ",line," password")     outf.write(s) inf.close() outf.close() 

i have tried:

for line in iter(inf):     outf.write("adduser " + lines + " password") 

output always:

passwordadduser user.name 

when read line file, include newline character. concatenate directly. in fact seeing this:

adduser line1\n passwordadduser line2\n passwordadduser line3\n 

etc.

to fix this, strip line when read it:

for line in iter(inf):     line = line.strip('\n')     s = "{0} {1} {2}\n" .format("adduser ",line," password") 

Comments