[issue42497] New pathlib.Path attribute

Steven D'Aprano report at bugs.python.org
Sun Nov 29 05:02:33 EST 2020


Steven D'Aprano <steve+python at pearwood.info> added the comment:

As a new feature, this could only go into 3.10, 3.9 is in feature freeze.

The requirements are unclear, it could mean any of:

- the file's *owner* has the execute bit set;

- the file's mode has any execute bit set;

- the *current user* has execute permission (regardless of whether they are the file owner, part of the file's group, or other);

- the file is an executable file (an app or exe) rather than a script that calls an executable file;

- the file is *not* on a file system that has disabled execution;

- if the file is double-clicked in a graphical shell, the file will run as a script or program (as opposed to merely open for reading);

etc.

So the concept of "is this file executable?" is more complex than it seems.

But you could start with getting the execute bit for the file's owner:


import pathlib
import stat
p = pathlib.Path('some file')
p.stat().st_mode & stat.S_IXUSR


or you could try this to find out whether the file *may* be executable for the current user:


os.access(p, os.X_OK)



but read the docs for some complications:

https://docs.python.org/3/library/os.html#os.access

----------
nosy: +steven.daprano
versions:  -Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42497>
_______________________________________


More information about the Python-bugs-list mailing list