1

I'm new to Python so please go easy, I'm sure my question has a very simple solution, that seems to be evading me. I'm trying to write a string to a file in this format:

"string1" variable "string2"

But it always seems to write "string2" "string1" variable

Here is my code:

inF = open("f:\test\Users.txt", 'r')
outF = open("f:\test\commands.txt", 'w')

for line in iter(inF):
    s = "{0} {1} {2}" .format("adduser ",line," password")
    outF.write(s)
inF.close()
outF.close()

I have also tried:

for line in iter(inF):
    outF.write("adduser " + lines + " password")

Output is always:

passwordadduser user.name
4
  • 1
    what are you callin iter on an iterator? Also there is no way you get any of the output you have posted Commented Jul 15, 2015 at 8:30
  • He's calling iter on a file. And the output makes sense, it's just not complete. Commented Jul 15, 2015 at 8:38
  • @garph0 a file is an iterator already, and the iter call is redundant, but not harmful. Anyway, I upvoted this and oppose closing it - it's indeed probably that the output lines mostly look like OP describes; just that the subtlety causing the problem has been missed. And isn't that par for the course? :) Commented Jul 15, 2015 at 8:42
  • Oooops (about the iter thing) :-) Commented Jul 15, 2015 at 8:50

3 Answers 3

3

When you read a line from a file, it will always include the newline character. You then concatenate that directly. So in fact what you are seeing is this:

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

etc.

To fix this, strip the line when you read it:

for line in iter(inF):
    line = line.strip('\n')
    s = "{0} {1} {2}\n" .format("adduser ",line," password")
Sign up to request clarification or add additional context in comments.

3 Comments

Don't just strip the newline from the input; you'll need to add one to the output lines as well :)
Also, @germn answer mentions context managers, which is a good thing.
All thank you very much for the help, the strip was indeed the issue, I'm a bit green so I did not know that the new line character was being read but I understand this now, I see that using context managers is the correct way to reference the files and will be reading a lot more to understand context management in more detail.
1

If you check, you'll see that the first two lines are like this:

adduser user1
 passwordadduser user2

You are reading the file line by line: line will contain a trailing "\n" so when you write it it goes to a new line, and you are not terminating what you write with a newline, so you get passwordadduser. Something like:

outF.write("{0} {1} {2}\n" .format("adduser ",line.strip()," password"))

Should fix your problem.

Comments

1

Please, use context managers to work with files. You should also handle new line symbols \n while reading and writing:

in_filename = "f:\\test\\Users.txt"
out_filename = "f:\\test\\commands.txt"

with open(in_filename, 'r') as in_fh, open(out_filename, 'w') as out_fh:
    for in_line in in_fh:
        out_line = "{} {} {}\n" .format("adduser", in_line.strip('\n'), "password")
        out_fh.write(out_line)

Users.txt:

us1
us2
us3

commands.txt:

adduser us1 password
adduser us2 password
adduser us3 password

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.