input from sdtin or file...
GrelEns
grelens at NOSPAMyahoo.NOTNEEDEDfr
Wed Mar 3 04:10:27 EST 2004
hello,
i wonder if there is a more pythonic way to do this : having input of a
script either from a file or from stdin if the file is not provided, see
below.
what do you think of it ? is there a better way ?
i have noticed some strange behaviour with stdin having sometimes to do 2 *
ctrl-z to end the input, or also stange temporizing when using os
redirection (like ./myscript.py <smallfile.txt) and script is delayed a lot,
is it a known issue ? or am i missing something ?
best wishes,
------
file_in = None
lines = None
try:
file_in = args[0]
except IndexError:
lines = sys.stdin
if file_in and not lines:
try:
lines = open(file_in, 'r')
except IOError:
sys.stderr.write("%s: %s: missing or invalid file argument\n" %
(globals()['_program'], file_in))
sys.exit(2)
for line in lines:
#do what i need
lines.close()
More information about the Python-list
mailing list