Mystery newline when reading from file using for loop

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.
shlingusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RichieHindleCommented:
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)
0
ramromconsultant Commented:
Or, text = data.split(".....\n")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shlingusAuthor 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.
0
shlingusAuthor 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...
0
RichieHindleCommented:
"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'
>>>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.