From 02a0222f346fa34280cdcb39ec7addf457e255f4 Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Sat, 2 Aug 2014 22:10:58 -0400 Subject: [PATCH] * wpylib.file.file_utils: added is_readable/is_writable test functions. --- file/file_utils.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/file/file_utils.py b/file/file_utils.py index 150af16..db29cb4 100644 --- a/file/file_utils.py +++ b/file/file_utils.py @@ -125,6 +125,21 @@ def is_executable_file(path): # Ref: http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python return os.path.isfile(path) and os.access(path, os.X_OK) +def is_writable(path): + """Determines whether a path exists and is writable by the current user, + like the `test -w' shell command. + """ + # Ref: http://stackoverflow.com/questions/2113427/determining-whether-a-directory-is-writeable + # Ref: http://stackoverflow.com/a/2113750/655885 + return os.access(path, os.W_OK) # W_OK is for writing, R_OK for reading, etc. + +def is_readable(path): + """Determines whether a path exists and is readable by the current user, + like the `test -r' shell command. + """ + return os.access(path, os.R_OK) + + def dirname2(path): """Returns the directory part of a path. The difference from os.path.dirname is that if the directory