File name from file descriptor?

Rainer Deyke root at rainerdeyke.com
Fri Jun 22 14:06:08 EDT 2001


"Carsten Gaebler" <clpy at snakefarm.org> wrote in message
news:3B335A1E.809B3FF7 at snakefarm.org...
> Carlos Ribeiro wrote:
>
> > functionality depend a lot on the OS being used. Could you please tell
us
> > why do you *need* to make it work using file descriptors?
>
> 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.

Even if 'gzip' accepts the first byte, it might not be a gzipped file.

> 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.

If you have enough memory, a better approach would be to read all of
'sys.stdin' into memory (with the 'read' method) and use the 'cStringIO'
module to create a stream.  Multiple 'cStringIO' streams can use the same
string as source.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list