<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>steffler's archive &#187; Uncategorized</title>
	<atom:link href="http://steffler.info/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://steffler.info</link>
	<description>bits of code i wrote and stuff about things and such.</description>
	<lastBuildDate>Tue, 18 May 2010 00:50:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Python code snippet: Read from stdin, write to file</title>
		<link>http://steffler.info/python-code-snippet-read-from-stdin-write-to-file/</link>
		<comments>http://steffler.info/python-code-snippet-read-from-stdin-write-to-file/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 00:20:01 +0000</pubDate>
		<dc:creator>steffler</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://steffler.info/?p=33</guid>
		<description><![CDATA[I was testing another script that was supposed to take the stdin and email it to me, and that script wasn&#8217;t working, so I wanted to verify that data was actually being passed into stdin in the first place so I could narrow down the problem. Here is a really simple snippet of code which [...]]]></description>
			<content:encoded><![CDATA[<p>I was testing another script that was supposed to take the stdin and email it to me, and that script wasn&#8217;t working, so I wanted to verify that data was actually being passed into stdin in the first place so I could narrow down the problem.</p>
<p>Here is a really simple snippet of code which you can invoke from a command line while piping stdin into it.   Example:
<pre>cat filename.txt | python thisscript.py</pre>
<p>Here is the script itself, along with comments to explain what it&#8217;s doing.</p>
<pre>#!/usr/local/bin/python
#
import sys
# the next line tells the script to read from stdin and use it for output
output = sys.stdin.read()
#the next line tells the script where to save the output
outfile = open('/tmp/file.txt', 'w')
#the next line tells the script to consider the file as stdout instead of the console
sys.stdout = outfile
#print the output from stdin into the file
print output
#close the file!
outfile.close()
</pre>
<p>This is useful if you are testing any application which sends email automatically using an external sendmail type application.  Tell the application to use this script instead of sendmail as the email application for example, and after sending a mail from the program,
<pre>/tmp/file.txt</pre>
<p> will contain the output of whatever the program was trying to email.  It is very useful in narrowing down problems from programs which interact with external scripts via pipes.  So, enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://steffler.info/python-code-snippet-read-from-stdin-write-to-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

