Skip to content
Advertisement

How to create a directory in root directory using python.pathlib.Path?

How do I create a directory in root directory using python.pathlib.Path?

>>> c = Path.home().parent / 'test'
>>> c
PosixPath('/home/test')
>>> c.mkdir()
Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    c.mkdir()
  File "/usr/lib/python3.6/pathlib.py", line 1248, in mkdir
    self._accessor.mkdir(self, mode)
  File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped
    return strfunc(str(pathobj), *args)
PermissionError: [Errno 13] Permission denied: '/home/test'

Advertisement

Answer

It is works if you run your script with sudo.

Or you can use os module:

import os
from pathlib import Path

c = str(Path.home().parent / 'test')
os.system(f"sudo mkdir {c}")
Advertisement