File name from file descriptor?

piet at cs.uu.nl piet at cs.uu.nl
Tue Jun 26 06:23:55 EDT 2001


>>>>> Carsten Gaebler <clpy at snakefarm.org> (CG) writes:

CG> 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?

CG> The problem is this: I have a script that is called like

CG> ./myscript.py < somefile

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

If you are sure that stdin will be a real file you can use:

sys.stdin.seek(0,0)
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl



More information about the Python-list mailing list