I am new to Python and keep getting the same permission denied error, when I execute the following statement:
>>> os.chdir('/root')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 13] Permission denied: '/root'
This means that the permissions on the /root
directory are set so that non-root users cannot access it.
So this is not really a program error nor a Python error, but a system configuration issue. One can debate the necessity of restricting the permissions like that. On my FreeBSD system you can read and access /root
; there are hardly any files there anyway. But maybe it contains sensitive data or programs on your system.
So you basically have two options;
sudo
or/root
.Which of the two is "best" depends on what you want to do and what is in the root directory.
But since the root
user has access to everything, it is possible to accidentally do a lot of damage when running as root
.
So in general it is recommended not to execute scripts as root, unless it is absolutely necessary.
BTW, it is possible to handle with this situation by testing for access first;
import os
if os.access('/root', os.R_OK|os.X_OK):
os.chdir('/root')
# whatever else you were going to do
else:
print 'Insufficient access to /root'