We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Mystery newline when reading from file using for loop

shlingus
shlingus asked
on
Medium Priority
514 Views
Last Modified: 2011-09-20
I'm getting a newline character at the start of a string when reading from a file and I don't know why.  My code looks like this:

data = open(file).read()
text = data.split(".....")

for t in text:
    cmd = 'program.exe ' + t
    pid = os.system(cmd)


The file I'm reading from has command line arguments for the program I'm trying to automate.  It contains something like this:

arg1 arg2 arg3 arg4
.....
arg1 arg2 arg3 arg4
.....
arg1 arg2 arg3 arg4

The problem I'm seeing is on the 2nd iteration of the for loop t gets a newline character at the beginning of it.  This messes up my program because the command line turns into:  

program.exe
arg1 arg2 arg3 arg4

Does anyone know where the newline is coming from and how I can get rid of it?  Maybe I'm going about this the wrong way and need to use a different solution.  Thanks in advance.
Comment
Watch Question

Imagine your input file with the newlines shown as \n - it would look like this:

arg1 arg2 arg3 arg4\n.....\narg1 arg2 arg3 arg4\n.....\n

So when you split on ".....", the newlines are left behind.

The safest fix is to strip all leading and trailing whitespace from the arguments, like this:

for t in text:
    cmd = 'program.exe ' + t.strip()
    pid = os.system(cmd)
consultant
Commented:
Or, text = data.split(".....\n")

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Using text = data.split(".....\n") was the way to go except I needed a '\r\n' at the end instead of just a '\n' because I'm running the script on Windows.  Thanks for the help, you answered my question.

Author

Commented:
Oh, and by the way, I never had any luck with the strip command.  It didn't get rid of the newlines.  Maybe it ignores \n or \r?  I don't know...
"I never had any luck with the strip command."  It returns a stripped copy of the string you apply it to - perhaps you were expecting it to work in-place?  This might explain things:

>>> x = "  string  \n"
>>> x
'  string  \n'
>>> x.strip()
'string'
>>> x
'  string  \n'
>>> x = x.strip()
>>> x
'string'
>>>
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.