This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: New pathlib.Path attribute
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Add to pathlib function to check permission similar to os.access
View: 42481
Assigned To: Nosy List: ThatXliner, steven.daprano
Priority: normal Keywords: patch

Created on 2020-11-29 03:54 by ThatXliner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
implement.patch ThatXliner, 2020-11-29 21:46 The patch to apply
Messages (6)
msg382034 - (view) Author: ThatXliner (ThatXliner) Date: 2020-11-29 03:54
Add an attribute to pathlib.Path testing if a file/directory/symlink is executable or the user has executable permissions.

I've been trying to find a pythonic way for this (preferably in pathlib.Path) but all I found was trips to the shell and back.

This issue proposes adding an attribute (which I haven't figured the name most would like but it would be somewhere near .can_execute or .is_exe)
msg382050 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-11-29 10:02
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
msg382082 - (view) Author: ThatXliner (ThatXliner) Date: 2020-11-29 18:13
Since pathlib.Path has module os synonyms, I think it would be reasonable to test with os.X_OK. Correct me if I'm wrong.

I think most people would want to check if the current user running the script could execute something.
msg382083 - (view) Author: ThatXliner (ThatXliner) Date: 2020-11-29 18:15
Also, an App (at least on MacOS) is technically an executable (I've ran iMessage from the command line before).
msg382084 - (view) Author: ThatXliner (ThatXliner) Date: 2020-11-29 18:18
Now I think of it, it would be weird to just add attributes testing for executable permissions. Why not add the attributes from os.access to pathlib.Path?

 - Path.can_execute = os.access(path, os.X_OK)
 - Path.can_read    = os.access(path, R_OK) 
...
msg382095 - (view) Author: ThatXliner (ThatXliner) Date: 2020-11-29 21:46
Ok, so I just made a patch implementing this using os.access. It adds the following methods to pathlib.Path

 - can_read    = Returns True if the user can read the path
 - can_write   = Returns True if the user can write on the path
 - can_execute = Returns True if the user can execute the path

These are implemented via

 - return os.access(str(self), os.R_OK)
 - return os.access(str(self), os.W_OK)
 - return os.access(str(self), os.X_OK)

respectively.
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86663
2020-11-30 18:58:45eryksunsetstatus: open -> closed
superseder: Add to pathlib function to check permission similar to os.access
resolution: duplicate
stage: resolved
2020-11-29 21:46:04ThatXlinersetfiles: + implement.patch
keywords: + patch
messages: + msg382095
2020-11-29 18:18:20ThatXlinersetmessages: + msg382084
2020-11-29 18:15:00ThatXlinersetmessages: + msg382083
2020-11-29 18:13:56ThatXlinersettype: enhancement
messages: + msg382082
2020-11-29 10:02:33steven.dapranosetnosy: + steven.daprano

messages: + msg382050
versions: - Python 3.9
2020-11-29 03:54:42ThatXlinercreate