Issue
I have a python 3 code:
system_name = 'myName'
path_perf_folder = os.path.dirname(sys.argv[0]) + '/' + system_name + '_test/'
try:
coriginal_umask = os.umask(0)
os.makedirs(path_perf_folder, 0o755)
finally:
os.umask(original_umask)
The code runs perfectly from python console (running directly os.makedirs command without permission and umask stuff), but when I run from Linux Centos 7.0 terminal or MacOS 10.14.1 terminal it does not work.
I have tried different permissions al well (0o770 and 0o777) but all the time my error is:
File "performance_maker.py", line 130, in <module>
os.makedirs(path_perf_folder, 0o755)
File "/shared/centos7/python/3.7.0/lib/python3.7/os.py", line 221, in
makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/myName_test/'
The umask part I c/p from a stackoverflow question but it does not work for me here is the link
Thanks!
Solution
os.path.dirname(sys.argv[0])
will only be a nonempty string if sys.argv[0]
has a path separator (i.e. '/' for unix-like systems) character in it. Using string operations to construct the path like you do means that you'll try to create the directory under /, which you probably don't have write access to. Instead, you should use os.path.join to construct your path, so that empty strings get handled properly and you get the relative path you want.
Answered By - manveti Answer Checked By - Marie Seifert (WPSolving Admin)