Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Marko Rauhamaa Newsgroups: comp.lang.python Subject: Re: fork/exec & close file descriptors Date: Tue, 02 Jun 2015 19:24:58 +0300 Organization: A noiseless patient Spider Lines: 25 Message-ID: <87eglu6pt1.fsf@elektro.pacujo.net> References: <87lhg26sfx.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="b7cb1518d23ec19d482dcc9c31d30fdd"; logging-data="32342"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NNnWNToQXxnF+TBVkfe2v" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:xmIZNXv9ZWMsz/ppF/L5dvCRIHM= sha1:5drncnzfDZQG564mwSVjEFDMNNI= Xref: csiph.com comp.lang.python:91857 Skip Montanaro : > On Tue, Jun 2, 2015 at 10:28 AM, Marko Rauhamaa wrote: >> >> The only problem is that you don't know how high you need to go in >> general. > > Sure, but I didn't know anyway, so no matter what upper bound I choose > (or what function I choose/implement), it's just going to be a guess. > os.closerange just codifies the straightforward procedure. Under linux, the cleanest way seems to be going through the files under /proc/self/fd: def close_fds(leave_open=[0, 1, 2]): fds = os.listdir(b'/proc/self/fd') for fdn in fds: fd = int(fdn) if fd not in leave_open: os.close(fd) No need for a upper bound. Marko