Problem with print and output to screen

Dave Angel d at davea.name
Tue Dec 11 17:48:09 EST 2012


On 12/11/2012 05:31 PM, Mike wrote:
> Hello, I am learning python and i have the next problem and i not understand how fix.
> The script is very simple, shows in the terminal the command but, the row is divided in two:
> Example:
>  
>
> /opt/zimbra/bin/zmprov ga user at example.com
> |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" 
> /opt/zimbra/bin/zmprov ga user2 at example.com
> |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
>
> And the correct is: 
> /opt/zimbra/bin/zmprov ga user at example.com |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)"
>
>
> The script is:
>
> #!/usr/bin/python
> import os
>
> for user in open ("email"):
>         print '/opt/zimbra/bin/zmprov ga ' + user + '|egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" '
>
>
>

I'll assume that 'email' is a file in the current working directory.  So
when  you open it and iterate through it, each line will be stored in
'user', including its trailing newline.

When you print 'user', you're seeing the newline.

To see for yourself, you could/should have used (just before your print
statement)

           print repr(user)

which will show such things as escape sequences.

Anyway, the easiest way to fix it would be to use rstrip() on the line.

for user in open("email"):
    user = user.rstrip()
    print '/opt/...

That's assuming there's no trailing whitespace that you DO want to preserve.



-- 

DaveA




More information about the Python-list mailing list