473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

stdin or optional fileinput

I was writing a small script the other day with the following CLI
prog [options] [file]*

I've used getopt to parse out the possible options, so we'll ignore
that part, and assume for the rest of the discussion that args is a
list of file names (if any provided).

I used this bit of code to detect wether i want stdinput or not.

if len(args)==0:
args = [ sys.stdin ]

Now in my main loop I've written:

for file in args:
for line in open( file ):
#do stuff

The probelm occurs when I pass no arguments and python trys to open(
sys.stdin ).
open() customarily accepts a pathname, and returns a file object.
But this code is so elegant I thought that maybe, if open were passed
file object it could re-open that file in a new mode (if a mode was
provided different from the current mode) or simply return the file
object it was passed.

Anyhow, what do you guys think? There might be something about my idea
that makes this proposed behavior of open() unexpected, and I just
haven't thought of it.

Mar 15 '06 #1
5 2469
the.theorist wrote:
I was writing a small script the other day with the following CLI
prog [options] [file]*

I've used getopt to parse out the possible options, so we'll ignore
that part, and assume for the rest of the discussion that args is a
list of file names (if any provided).

I used this bit of code to detect wether i want stdinput or not.

if len(args)==0:
args = [ sys.stdin ]

Now in my main loop I've written:

for file in args:
for line in open( file ):
#do stuff


You should probably write:

if not args: # note that len(args) == 0 is repetitively redundant, over
# and over again, in a reiterative manner
files = [sys.stdin]
else:
files = (open(filename) for filename in args)

....

for fileobj in files: # using the name 'file' is a bad idea since it
# shadows the builtin 'file'
for line in fileobj:
# do stuff
STeVe
Mar 15 '06 #2

Steven Bethard wrote:
the.theorist wrote:
I was writing a small script the other day with the following CLI
prog [options] [file]*

I've used getopt to parse out the possible options, so we'll ignore
that part, and assume for the rest of the discussion that args is a
list of file names (if any provided).

I used this bit of code to detect wether i want stdinput or not.

if len(args)==0:
args = [ sys.stdin ]

Now in my main loop I've written:

for file in args:
for line in open( file ):
#do stuff


You should probably write:

if not args: # note that len(args) == 0 is repetitively redundant, over
# and over again, in a reiterative manner
files = [sys.stdin]
else:
files = (open(filename) for filename in args)

...

for fileobj in files: # using the name 'file' is a bad idea since it
# shadows the builtin 'file'
for line in fileobj:
# do stuff
STeVe


I'll keep both those in mind for future programs.
my current fix has been

if not args:
args = [ sys.stdin ]
else:
map( open, args )

and then a modification to the main loop, as you proposed.

I thought that one day I might run into a problem opening too many
files though (esp if i used xargs in a pipe). And it just _feels_
better to open and close as I loop over the files list, rather than
open everything at the get go.

OH, i've noticed you used a generator that takes care of that. Except,
the machine I'm working on is currently stuck at Python 2.3. Still,
I'll keep your suggestions in mind for future coding.

Mar 15 '06 #3
the.theorist wrote:
Steven Bethard wrote:
the.theorist wrote:
I was writing a small script the other day with the following CLI
prog [options] [file]*

I've used getopt to parse out the possible options, so we'll ignore
that part, and assume for the rest of the discussion that args is a
list of file names (if any provided).

I used this bit of code to detect wether i want stdinput or not.

if len(args)==0:
args = [ sys.stdin ]

Now in my main loop I've written:

for file in args:
for line in open( file ):
#do stuff
You should probably write:

if not args: # note that len(args) == 0 is repetitively redundant, over
# and over again, in a reiterative manner
files = [sys.stdin]
else:
files = (open(filename) for filename in args)

...

for fileobj in files: # using the name 'file' is a bad idea since it
# shadows the builtin 'file'
for line in fileobj:
# do stuff
STeVe

I'll keep both those in mind for future programs.
my current fix has been

if not args:
args = [ sys.stdin ]
else:
map( open, args )

and then a modification to the main loop, as you proposed.

I thought that one day I might run into a problem opening too many
files though (esp if i used xargs in a pipe). And it just _feels_
better to open and close as I loop over the files list, rather than
open everything at the get go.

Yes, that feels most Pythonic.
OH, i've noticed you used a generator that takes care of that. Except,
the machine I'm working on is currently stuck at Python 2.3. Still,
I'll keep your suggestions in mind for future coding.

Well the nice thing about the generator expression is precisely its
deferral of applying open() to the filename argument until the iterable
element is extracted. But as you observe there's no use trying that in
Python 2.3, so you'll have to adopt a slightly less elegant solution for
now.

The important thing is that you seem to be well in touch with the
various issues, so there's a good chance you won't go wrong.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

Mar 15 '06 #4
"the.theorist" wrote:
I used this bit of code to detect wether i want stdinput or not.

if len(args)==0:
args = [ sys.stdin ]

Now in my main loop I've written:

for file in args:
for line in open( file ):
#do stuff

The probelm occurs when I pass no arguments and python trys to open(
sys.stdin ).
open() customarily accepts a pathname, and returns a file object.
But this code is so elegant I thought that maybe, if open were passed
file object it could re-open that file in a new mode (if a mode was
provided different from the current mode) or simply return the file
object it was passed.


so use your own open function:

def myopen(file):
if isinstance(file, basestring):
return open(file)
return file

for file in args:
for line in myopen( file ):
#do stuff
or perhaps

def myopen(file):
if hasattr(file, "read"):
return file
return open(file)

...

or perhaps

if not args:
args = [ "-" ]

def myopen(file):
if file == "-":
return sys.stdin
return open(file)

for file in args:
for line in myopen( file ):
#do stuff

(the "-" = stdin convention is quite common)

</F>

Mar 15 '06 #5
the.theorist wrote:
I'll keep both those in mind for future programs.
my current fix has been

if not args:
****args*=*[*sys.stdin*]
else:
****map(*open,*args*)

and then a modification to the main loop, as you proposed.

I thought that one day I might run into a problem opening too many
files though (esp if i used xargs in a pipe). And it just feels
better to open and close as I loop over the files list, rather than
open everything at the get go.

OH, i've noticed you used a generator that takes care of that. Except,
the machine I'm working on is currently stuck at Python 2.3. Still,
I'll keep your suggestions in mind for future coding.


use

if args:
args = itertools.imap(open, args)
else:
args = [sys.stdin]

to get the same lazy-open behaviour as with the generator expression.
Or have a look at http://docs.python.org/lib/module-fileinput.html.

Peter
Mar 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Daniel Yoo | last post by:
Hi everyone, I'm was wondering: would it be a good idea to have the FileInput class support a read() method? I found myself running into a small problem while using xml.sax.parse in combination...
2
by: Jon Schull | last post by:
I want to write a filter that will take input from files on the command line and/or from stdin. But if nothing has been piped to stdin, I don't want my program to hang--I want it to do something...
8
by: max(01)* | last post by:
hi. i was wondering, what's the simplest way to echo the standard input to the standard output, with no modification. i came up with: .... while True: try:
7
by: Will McDonald | last post by:
Hi all. I'm writing a little script that operates on either stdin or a file specified on the command line when run. I'm trying to handle the situation where the script's run without any input...
6
by: cyberco | last post by:
Using fileinput.input('test.txt') I probably forgot to process all lines or so, since I get the error 'input() already active' when i try to call fileinput.input('test.txt') again. But how can I...
10
by: wo_shi_big_stomach | last post by:
Newbie to python writing a script to recurse a directory tree and delete the first line of a file if it contains a given string. I get the same error on a Mac running OS X 10.4.8 and FreeBSD 6.1. ...
4
by: Adam Funk | last post by:
I'm using this sort of standard thing: for line in fileinput.input(): do_stuff(line) and wondering whether it reads until it hits an EOF and then passes lines (one at a time) into the...
11
by: jo3c | last post by:
hi everybody im a newbie in python i need to read line 4 from a header file using linecache will crash my computer due to memory loading, because i am working on 2000 files each is 8mb ...
3
by: Robert | last post by:
I would like to count lines in a file using the fileinput module and I am getting an unusual output. ------------------------------------------------------------------------------...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.