Linux服务器/ Python:OSError:[Errno 13]权限被拒绝
我已经尝试在Python中编写一个程序,该程序需要一些值,创建一个目录并将其添加到文本文件中供以后使用。我已经把它上传到了Ubuntu的VPS服务器上,因为我打算在以后的日子与我的网站一起使用它。但是,每当我运行的代码(下)我得到以下错误:Linux服务器/ Python:OSError:[Errno 13]权限被拒绝
Traceback (most recent call last):
File "fileCreator.py", line 13, in <module>
os.mkdir(dirName)
OSError: [Errno 13] Permission denied: 'just-a-test'
Python代码:
#!/usr/src
import os
from distutils.dir_util import copy_tree
import sys
title = raw_input("Blog Title: ")
dirName = title.replace(" ", "-").lower()
if os.path.isdir(dirName):
print("Error: Directory Exists")
sys.exit()
else:
os.mkdir(dirName)
copy_tree("page", dirName)
def assignment(title, dirName):
desc = raw_input("Article Description: ")
fo = open(dirName + "/txt-files/title.txt", "w")
fo.write(title)
fo.close()
fo = open(dirName + "/txt-files/desc.txt", "w")
fo.write(desc)
fo.close()
return None
assignment(title, dirName)
print("Done")
这是某种形式的权限错误的,我已经看到了它的几个其他主题但他们都没有得出解决方案。我不太熟练使用Linux命令,所以不会用!非常感谢帮助!
TLDR;用Python脚本在目录中运行chmod 744
。
对于您尝试创建文件夹的目录,您没有正确的权限。从已fileCreator.py同一目录下,运行在命令行上ls -la .
,它会输出是这样的:
drwxr-xr-x 9 user staff 306 Oct 9 21:29 .
drwxr-xr-x+ 36 user staff 1224 Sep 28 12:26 ..
-rw-r--r-- 1 user staff 977 Oct 9 21:04 .bashrc
,可能一堆其他文件。第一行是当前目录。 user
是您的登录名,staff
是拥有它的组。他们在你的系统上会有所不同。 drwxr-xr-x
是权限,它们由chmod
命令更改。
签出更多的关于Linux的权限在这里:https://www.linux.com/learn/understanding-linux-file-permissions
啊,这太棒了,谢谢。我以前尝试检查目录用户,看看这会改变什么,但744作品,所以谢谢! –
import os
def create_assignment_directory(path):
"""
create given path in local filesystem
given path can be a relative, absolute path or
path using tilde symbols like '~/folder_in_home_directory'
prints absolute, normalized path for debugging purposes
"""
expanded_path = os.path.expanduser(path)
normalized_path = os.path.abspath(expanded_path)
print("create directory {0}".format(normalized_path))
try:
os.mkdir(expanded_path)
except OSError as e:
if e.errno == 17: # errno.EEXIST
print("directory {0} already exists.".format(normalized_path))
else:
print("successfully created directory {0}".format(normalized_path))
print("current working directory {0}".format(os.getcwd()))
create_assignment_directory("just-a-test")
create_assignment_directory("~/just-a-test")
create_assignment_directory("/tmp/just-a-test")
打印调试当前工作目录。 'os.getcwd()' – harandk
减少你的问题/代码来管理受限环境中的文件系统资源。从相关问题中学习解答。 –
@SaschaGottfried你是什么意思? –