Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95835

Re: Low level file descriptors and high-level Python files

Path csiph.com!news.swapon.de!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <random832@fastmail.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'practice,': 0.07; 'read-only': 0.07; 'subject:file': 0.07; 'descriptor': 0.09; 'mode,': 0.09; 'oserror': 0.09; 'received:internal': 0.09; 'subject:files': 0.09; 'file?': 0.16; 'message-id:@webmail.messagingengine.com': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:messagingengine.com': 0.16; 'with?': 0.16; 'wrote:': 0.16; 'sep': 0.22; 'suppose': 0.22; 'code.': 0.23; 'passing': 0.23; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'equivalent': 0.27; 'not.': 0.27; 'object,': 0.27; 'inspect': 0.29; 'raise': 0.29; "i'm": 0.30; 'probably': 0.31; 'call,': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'open': 0.33; 'tue,': 0.34; 'file': 0.34; 'next': 0.35; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'there': 0.36; 'mode': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'method': 0.37; 'received:66': 0.38; 'test': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'from:no real name:2**0': 0.60; 'your': 0.60; 'close': 0.61; 'header:Message- Id:1': 0.61; 'production': 0.67; 'writing,': 0.91
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.us; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=WayojQhuH6xtFgxGluVIPnGbmuA=; b=Hp+IxT u678zyroHYO2c1dalYGnXo7P8e9amFxCPfJBUFJXkBfJLAet5cWBQV+Xcwux8vEF W+X3KLaST+E7vy3Z0xP2e7cClffV3hLi6KlI6470N4aOuGJZPJcBDsq/GCKNBB+M JI8hdrHFwabVFwQ8MAPOy+H9Z13djkduavISQ=
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=WayojQhuH6xtFgx GluVIPnGbmuA=; b=gV6jqSp+t3/oFVLiYVqy5cWurndcbyFXXDrQLdN2oD1ApKQ HJ7h+9uK6yd3YOwbBb5XTte9S05opa7In/7WAHiYRPerwVlCC9CofmUUtmWyUojq O7Rh7Jtt2VibqoMqaZxFIHNPHydIymDtZuVQ+ZcSSPmo77PUCTerZvBwPWQ4=
X-Sasl-Enc fRhUQ0Qn2IbYaEFhqhtGjnU4WTVQ6LZHhJ2uDkvDEq5S 1441122964
From random832@fastmail.us
To python-list@python.org
MIME-Version 1.0
Content-Transfer-Encoding 7bit
Content-Type text/plain
X-Mailer MessagingEngine.com Webmail Interface - ajax-4500bf93
Subject Re: Low level file descriptors and high-level Python files
Date Tue, 01 Sep 2015 11:56:04 -0400
In-Reply-To <55e5bcd3$0$1639$c3e8da3$5496439d@news.astraweb.com>
References <55e5bcd3$0$1639$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.46.1441122966.23514.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1441122966 news.xs4all.nl 23792 [2001:888:2000:d::a6]:32845
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:95835

Show key headers only | View raw


On Tue, Sep 1, 2015, at 10:57, Steven D'Aprano wrote:
> Q1:  In this example, I know that I opened the fd in write mode, because
> I
> did it myself. But since I'm not actually opening it, how do I know what
> mode to use in the call to fdopen? Is there something I can call to find
> out what mode a file descriptor has been opened with?

In principle, you can find out with fcntl.

In practice, don't you already know what kind of processing you intend
to do with the file? If your "processing" involves writing, just try
writing to it, and if it doesn't work then it's the caller's fault for
passing in a read-only file handle.

> Now let's suppose I solve that problem, process the file_obj, and close
> it:
> 
> file_obj.close()
> 
> Q2:  Do I still have to close the file descriptor with os.close(fd)? 
> (I think not.)

You do not.

> Q3:  I could probably answer Q2 myself if I knew how to check whether a
> fd
> was open or not. With a file object, I can inspect file_obj.closed and it
> will tell me whether the file is open or not. Is there an equivalent for
> file descriptors?

Well, if you try to call os.close, or any other operation for that
matter, it will raise an OSError with errno=EBADF.

Note that if the file _has_ been closed it may be reused by the next
open call, so it's best not to use this test method in production code.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Low level file descriptors and high-level Python files Steven D'Aprano <steve@pearwood.info> - 2015-09-02 00:57 +1000
  Re: Low level file descriptors and high-level Python files random832@fastmail.us - 2015-09-01 11:56 -0400
  Re: Low level file descriptors and high-level Python files Laura Creighton <lac@openend.se> - 2015-09-01 19:41 +0200
    Re: Low level file descriptors and high-level Python files Grant Edwards <invalid@invalid.invalid> - 2015-09-01 22:19 +0000
      Re: Low level file descriptors and high-level Python files Laura Creighton <lac@openend.se> - 2015-09-02 09:02 +0200

csiph-web