piping into a python script

Hexamorph hexamorph at gmx.net
Thu Jan 24 18:53:22 CET 2008


Donn Ingle wrote:
> Paddy wrote:
>> fileinput is set to process each file a line at a time unfortunately.
> Wow. So there seems to be no solution to my OP. I'm amazed, I would have
> thought a simple list of strings, one from stdin and one from the args,
> would be easy to get.
> 
> I *really* don't want to open each file, that would be insane.
> 
> Perhaps I shall have to forgo the stdin stuff then, after all.
> 

Hi!

I'm not sure if I completely get what you want, but what's about this:

#!/usr/bin/python

import sys

filelist = []
with_stdin=0

if len(sys.argv) > 1:
	for file in sys.argv[1:]:
		if file == "-":
			with_stdin=1
			continue
		filelist.append(file)
else:
	with_stdin=1

if with_stdin:
	 for file in sys.stdin:
		 filelist.append(file)


for file in filelist:
	print "Processing file: %s" % file



It's a bit clumsy, but seems to do what I guess you want.


HTH



More information about the Python-list mailing list