<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>
  <title>Linux Format forums</title>
  <link>http://www.linuxformat.com/forums/index.php</link>
  <description>Help, discussion, magazine feedback and more</description>
  <language>english</language>
  <copyright>(c) Copyright Thu May 23, 2013 8:07 am by Linux Format forums</copyright>
  <managingEditor>webmaster@linuxformat.com</managingEditor>
  <webMaster>webmaster@linuxformat.com</webMaster>
  <pubDate>Thu May 23, 2013 8:07 am</pubDate>
  <lastBuildDate>Thu May 23, 2013 8:07 am</lastBuildDate>
  <docs>http://backend.userland.com/rss</docs>
  <generator>phpBB2 RSS Syndication Mod by Lucas</generator>
  <ttl>1</ttl>

  <image>
    <title>Linux Format forums</title>
    <url></url>
    <link>http://www.linuxformat.com/forums/</link>
    <description>Help, discussion, magazine feedback and more</description>
  </image>

                                      <item>
                                        <title>Re: Opening and processing files in Python</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=9465#9465</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=5'&gt;nelz&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Tue Oct 04, 2005 2:01 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Because the code doesn't look like line noise &lt;img src=&quot;images/smiles/icon_smile.gif&quot; alt=&quot;Smile&quot; border=&quot;0&quot; /&gt;</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=9465#9465</comments>
                                        <author>nelz</author>
                                        <pubDate>Tue Oct 04, 2005 2:01 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=9465#9465</guid>
                                      </item>
                                      <item>
                                        <title>Re: Opening and processing files in Python</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=9464#9464</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=619'&gt;PhilC&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Tue Oct 04, 2005 1:51 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Thanks overflow,&lt;br /&gt;
That was a very insightful answer and a great example to demonstrate.&lt;br /&gt;
(Still wondering why people prefer to use  this to perl though)</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=9464#9464</comments>
                                        <author>PhilC</author>
                                        <pubDate>Tue Oct 04, 2005 1:51 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=9464#9464</guid>
                                      </item>
                                      <item>
                                        <title>Re: Opening and processing files in Python</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=7345#7345</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=2827'&gt;overflow&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Aug 24, 2005 1:00 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Python doesn't have the equivalent of the Perl &amp;lt;&amp;gt; operator but it's easy enough to reproduce it. Here's a simple cat program written in Python which supports multiple filenames on the command line and '-' for stdin.&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;#! /usr/bin/python&lt;br /&gt;
import sys&lt;br /&gt;
def fileargs&amp;#40;&amp;#41;&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;for filename in sys.argv&amp;#91;1&amp;#58;&amp;#93;&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;if filename == '-'&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;for line in sys.stdin&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;yield line&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;else&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;for line in file&amp;#40;filename&amp;#41;&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;yield line&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;br /&gt;
for line in fileargs&amp;#40;&amp;#41;&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;print line&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
The fileargs function is an iterator (or generator, I can't remember the difference) and passes back records from each input file in turn. It switches to stdin if '-' is specified.&lt;br /&gt;
&lt;br /&gt;
I hope that helps.</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=7345#7345</comments>
                                        <author>overflow</author>
                                        <pubDate>Wed Aug 24, 2005 1:00 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=7345#7345</guid>
                                      </item>
                                      <item>
                                        <title>Re: Opening and processing files in Python</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=7344#7344</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=2827'&gt;overflow&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Aug 24, 2005 12:40 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      It is even easier than the example posted above.&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
for line in file&amp;#40;'foo.bar'&amp;#41;&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; print line&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
file (which is a synonym for open) support iterators and it yields a class which simply passes records back to you one at a time.&lt;br /&gt;
&lt;br /&gt;
The example posted above would read the whole file into memory in the readlines() function which might be a performance problem for large files.</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=7344#7344</comments>
                                        <author>overflow</author>
                                        <pubDate>Wed Aug 24, 2005 12:40 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=7344#7344</guid>
                                      </item>
                                      <item>
                                        <title>RE: Opening and processing files in Python</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=6978#6978</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=619'&gt;PhilC&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Thu Aug 18, 2005 1:34 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Thanks for getting me started nelz, I'll be using that a lot in the future.&lt;br /&gt;
&lt;br /&gt;
I did a little bit more digging though since the perl diamond operator can do a little bit more than this code.&lt;br /&gt;
i.e. it will read each file given as an argument to the program and process each file in turn.  Much more useful than that though is that it can read input from stdin or a pipe.&lt;br /&gt;
&lt;br /&gt;
Just in case anyone else is looking for this feature in Python, I had another look around python.org and found this code: (http://www.python.org/doc/2.2.3/lib/module-fileinput.html)&lt;br /&gt;
&lt;br /&gt;
import fileinput&lt;br /&gt;
for line in fileinput.input():&lt;br /&gt;
        dosomethingwith (line)</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=6978#6978</comments>
                                        <author>PhilC</author>
                                        <pubDate>Thu Aug 18, 2005 1:34 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=6978#6978</guid>
                                      </item>
                                      <item>
                                        <title>RE: Opening and processing files in Python</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=6918#6918</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=5'&gt;nelz&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Aug 17, 2005 6:35 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      &lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
lines = open&amp;#40;'foo/bar'&amp;#41;.readlines&amp;#40;&amp;#41;&lt;br /&gt;
for line in lines&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;dosomethingwith&amp;#40;line&amp;#41;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=6918#6918</comments>
                                        <author>nelz</author>
                                        <pubDate>Wed Aug 17, 2005 6:35 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=6918#6918</guid>
                                      </item>
                                      <item>
                                        <title>Opening and processing files in Python</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=6907#6907</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=619'&gt;PhilC&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Aug 17, 2005 4:16 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Hi,&lt;br /&gt;
&lt;br /&gt;
I'm trying to make a start in Python programming.  I consider myself competant with Perl, and I want to do some of the same things which are easy in Perl, in Python.&lt;br /&gt;
&lt;br /&gt;
In particular, I want to be open a file and process each line at a time.&lt;br /&gt;
In Perl I just do:&lt;br /&gt;
while (&amp;lt;&amp;gt;) {&lt;br /&gt;
    doSomethingWith ($_);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
I've had a browse around python.org, but can't find any good references to opening and processing files.&lt;br /&gt;
&lt;br /&gt;
Can anyone tell me how to do the above Perl code in Python?  I really would appreciate it, thanks.</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=6907#6907</comments>
                                        <author>PhilC</author>
                                        <pubDate>Wed Aug 17, 2005 4:16 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=6907#6907</guid>
                                      </item></channel></rss>