简体   繁体   中英

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'

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}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM