Ben Hoyt
2014-06-10 04:02:14 UTC
Received: from localhost (HELO mail.python.org) (127.0.0.1)
by albatross.python.org with SMTP; 10 Jun 2014 06:02:21 +0200
Received: from mail-qc0-x231.google.com (unknown
[IPv6:2607:f8b0:400d:c01::231])
(using TLSv1 with cipher ECDHE-RSA-AES128-SHA (128/128 bits))
(No client certificate requested)
by mail.python.org (Postfix) with ESMTPS
for <python-***@python.org>; Tue, 10 Jun 2014 06:02:20 +0200 (CEST)
Received: by mail-qc0-f177.google.com with SMTP id i17so1624297qcy.22
for <python-***@python.org>; Mon, 09 Jun 2014 21:02:14 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
h=mime-version:date:message-id:subject:from:to:content-type;
bh=+AsoZKcj985DIwzl0nEJcqp5ACkhZFgu7zr6djRUaY0=;
b=hNPPtb7HGMVLJsjdFj6OQBVtYIEKTI0tUDTm3Lrhrm0IjywNP9p78A6n3fqehEZH9D
iKPUNZvWsyBtzfEqia8rTOvBW8NNQKM56IMKMznz38Jcw/tn6PBTnBLSb/2Yybbxw2qO
DNvlcysHw1yKI8kbuNOXF6vwK1YZ430ZZsm3zibM3kKaMZxOefLxHjmFPFWGcgtwo/Wa
eu1oEJIYQhK9kqLd9jFiFEe/kmVYiWE8kckrl50aYNJccbmGSnCh8AQX+OEYTsnbSWJh
+lJZTz/V0xxcwjoiW7ypwV3BQpUjhuQhdKlqqe09aiE8n4S8jzJEMGI3jKXbD6mN9fER
M8mw==
X-Received: by 10.140.29.34 with SMTP id a31mr36278811qga.95.1402372934509;
Mon, 09 Jun 2014 21:02:14 -0700 (PDT)
Received: by 10.229.174.6 with HTTP; Mon, 9 Jun 2014 21:02:14 -0700 (PDT)
X-BeenThere: python-***@python.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: Python core developers <python-dev.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-dev>,
<mailto:python-dev-***@python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-dev/>
List-Post: <mailto:python-***@python.org>
List-Help: <mailto:python-dev-***@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-dev>,
<mailto:python-dev-***@python.org?subject=subscribe>
Errors-To: python-dev-bounces+python-python-dev=***@python.org
Sender: "Python-Dev"
<python-dev-bounces+python-python-dev=***@python.org>
Archived-At: <http://permalink.gmane.org/gmane.comp.python.devel/148165>
Hi folks,
As pointed out to me recently in an issue report [1] on my scandir
module, Python's os.stat() simply discards most of the file attribute
information fetched via the Win32 system calls. On Windows, os.stat()
calls CreateFile to open the file and get the dwFileAttributes value,
but it throws it all away except the FILE_ATTRIBUTE_DIRECTORY and
FILE_ATTRIBUTE_READONLY bits. See CPython source at [2].
Given that os.stat() returns extended, platform-specific file
attributes on Linux and OS X platforms (see [3] -- for example,
st_blocks, st_rsize, etc), it seems that Windows is something of a
second-class citizen here.
There are several questions on StackOverflow about how to get this
information on Windows, and one has to resort to ctypes. For example,
[4].
To solve this problem, what do people think about adding an
"st_winattrs" attribute to the object returned by os.stat() on
Windows?
Then, similarly to existing code like hasattr(st, 'st_blocks') on
Linux, you could write a cross-platform function to determine if a
file was hidden, something like so:
FILE_ATTRIBUTE_HIDDEN = 2 # constant defined in Windows.h
def is_hidden(path):
if startswith(os.path.basename(path), '.'):
return True
st = os.stat(path)
if hasattr(st, 'st_winattrs') and st.st_winattrs & FILE_ATTRIBUTE_HIDDEN:
return True
return False
I'd be interested to hear people's thoughts on this.
Thanks,
Ben.
[1]: https://github.com/benhoyt/scandir/issues/22
[2]: https://github.com/python/cpython/blob/master/Modules/posixmodule.c#L1462
[3]: https://docs.python.org/3.4/library/os.html#os.stat
[4]: http://stackoverflow.com/a/6365265
by albatross.python.org with SMTP; 10 Jun 2014 06:02:21 +0200
Received: from mail-qc0-x231.google.com (unknown
[IPv6:2607:f8b0:400d:c01::231])
(using TLSv1 with cipher ECDHE-RSA-AES128-SHA (128/128 bits))
(No client certificate requested)
by mail.python.org (Postfix) with ESMTPS
for <python-***@python.org>; Tue, 10 Jun 2014 06:02:20 +0200 (CEST)
Received: by mail-qc0-f177.google.com with SMTP id i17so1624297qcy.22
for <python-***@python.org>; Mon, 09 Jun 2014 21:02:14 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
h=mime-version:date:message-id:subject:from:to:content-type;
bh=+AsoZKcj985DIwzl0nEJcqp5ACkhZFgu7zr6djRUaY0=;
b=hNPPtb7HGMVLJsjdFj6OQBVtYIEKTI0tUDTm3Lrhrm0IjywNP9p78A6n3fqehEZH9D
iKPUNZvWsyBtzfEqia8rTOvBW8NNQKM56IMKMznz38Jcw/tn6PBTnBLSb/2Yybbxw2qO
DNvlcysHw1yKI8kbuNOXF6vwK1YZ430ZZsm3zibM3kKaMZxOefLxHjmFPFWGcgtwo/Wa
eu1oEJIYQhK9kqLd9jFiFEe/kmVYiWE8kckrl50aYNJccbmGSnCh8AQX+OEYTsnbSWJh
+lJZTz/V0xxcwjoiW7ypwV3BQpUjhuQhdKlqqe09aiE8n4S8jzJEMGI3jKXbD6mN9fER
M8mw==
X-Received: by 10.140.29.34 with SMTP id a31mr36278811qga.95.1402372934509;
Mon, 09 Jun 2014 21:02:14 -0700 (PDT)
Received: by 10.229.174.6 with HTTP; Mon, 9 Jun 2014 21:02:14 -0700 (PDT)
X-BeenThere: python-***@python.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: Python core developers <python-dev.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-dev>,
<mailto:python-dev-***@python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-dev/>
List-Post: <mailto:python-***@python.org>
List-Help: <mailto:python-dev-***@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-dev>,
<mailto:python-dev-***@python.org?subject=subscribe>
Errors-To: python-dev-bounces+python-python-dev=***@python.org
Sender: "Python-Dev"
<python-dev-bounces+python-python-dev=***@python.org>
Archived-At: <http://permalink.gmane.org/gmane.comp.python.devel/148165>
Hi folks,
As pointed out to me recently in an issue report [1] on my scandir
module, Python's os.stat() simply discards most of the file attribute
information fetched via the Win32 system calls. On Windows, os.stat()
calls CreateFile to open the file and get the dwFileAttributes value,
but it throws it all away except the FILE_ATTRIBUTE_DIRECTORY and
FILE_ATTRIBUTE_READONLY bits. See CPython source at [2].
Given that os.stat() returns extended, platform-specific file
attributes on Linux and OS X platforms (see [3] -- for example,
st_blocks, st_rsize, etc), it seems that Windows is something of a
second-class citizen here.
There are several questions on StackOverflow about how to get this
information on Windows, and one has to resort to ctypes. For example,
[4].
To solve this problem, what do people think about adding an
"st_winattrs" attribute to the object returned by os.stat() on
Windows?
Then, similarly to existing code like hasattr(st, 'st_blocks') on
Linux, you could write a cross-platform function to determine if a
file was hidden, something like so:
FILE_ATTRIBUTE_HIDDEN = 2 # constant defined in Windows.h
def is_hidden(path):
if startswith(os.path.basename(path), '.'):
return True
st = os.stat(path)
if hasattr(st, 'st_winattrs') and st.st_winattrs & FILE_ATTRIBUTE_HIDDEN:
return True
return False
I'd be interested to hear people's thoughts on this.
Thanks,
Ben.
[1]: https://github.com/benhoyt/scandir/issues/22
[2]: https://github.com/python/cpython/blob/master/Modules/posixmodule.c#L1462
[3]: https://docs.python.org/3.4/library/os.html#os.stat
[4]: http://stackoverflow.com/a/6365265