一个非常简单的代码来创建一个目录:
ssh_path = f"{os.getenv('HOME')}/temp/.ssh"
print(ssh_path)
os.mkdir(ssh_path)
它打印目录的路径并以错误结束
FileNotFoundError: [Errno 2] No such file or directory: '/Users/myusername/temp/.ssh'
/Users/myusername/temp/.ssh
最佳答案
使用 os.makedirs()
创建中间文件夹。
os.makedirs(ssh_path)
如果您使用
Path
来自 pathlib
, 你可以做:ssh_path = f"{os.getenv('HOME')}/temp/.ssh")
ssh = Path(ssh_path)
ssh.mkdir(parents=true)
关于python-3.x - os.mkdir() 返回错误 "FileNotFoundError: [Errno 2] No such file or directory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59181118/