File name from file descriptor?

Donn Cave donn at u.washington.edu
Fri Jun 22 12:31:02 EDT 2001


Quoth Carsten Gaebler <clpy at snakefarm.org>:
| The problem is this: I have a script that is called like
|
| ./myscript.py < somefile
|
| where somefile is a text file which may or may not be gzipped. To
| determine whether or not the data is gzipped I read in one byte via the
| gzip module. If that raises an exception I know the data is not gzipped.
| But I'd miss the first byte if it is gzipped. Yes, I could store this byte
| somewhere and then pass it around somehow, but ... you know? :-) So I'd
| like to open a second 'instance' of the file for reading but I only have
| sys.stdin's file descriptor.

The only decent way to do this is with a buffered input stream,
If you could honestly say it will will always be a disk file,
you could seek back to where you started after reading the header.

Python's built in file object is a buffered input stream, but
it's based on C library stdio and doesn't have the features you
need.  It would be kind of a big job to write a replacement,
just for the present application, but a full replacement for
the file object with more access to the buffer would be a nice
addition to the standard library.  Not only for "peeking" at
input, but also for things like select (you want to know if
there's input, but you can't tell if you have input already
buffered.)

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list