[Tutor] subprocess adds %0A to end of string

Martin Walsh mwalsh at mwalsh.org
Mon Dec 22 06:18:11 CET 2008


David wrote:
> Martin Walsh wrote:
> 
>> Welcome!
> 
> thanks

welcome (uh oh, infinite loop warning)

> This "podcast_file.write('%s: %s' % (entry.updated, entry.link))"
> writes it in one very long string

Copy and paste gets me every time. Try this, and note the presence of
the newline ('\n'):

podcast_file.write('%s: %s\n' % (entry.updated, entry.link))

> 
> The Latest Link
> http://linuxcrazy.com/podcasts/LC-44-arne.ogghttp://linuxcrazy.com/podcas=>>
> 
> 
> and sys.stdout prints to the file one line at a time

The primary issue is not that you're writing to sys.stdout, it's that
you're using the print statement which implicitly adds a newline.
Technically you can also do the following (python <= 2.5, and maybe 2.6)
which should demonstrate the concept...

podcastfile = file(somepath, 'a')
print >> podcastfile, '%s: %s' (entry.updated, entry.link)

... however, I would recommend against using the above print statement
syntax for anything other than experimentation because 1) it's not
common practice IMHO, and 2) it's gone starting with python 3.0. 'print'
will be a builtin function instead of a statement going forward.

I second Alan's recommendation to read a tutorial or two to solidify
your understanding of the basic concepts. Alan's tutorial is very good,
and covers everything we have discussed so far, particularly the
"Handling Files" section. Check the link in his email sig.

HTH,
Marty


More information about the Tutor mailing list