[英]Linux Server/Python: OSError: [Errno 13] Permission denied
我試過用Python編寫程序,該程序需要一些值,創建目錄並將其添加到文本文件中,以備后用。 我已將其上傳到ubuntu VPS服務器,以備日后在我的網站上使用。 但是,每當我運行下面的代碼時,都會出現以下錯誤:
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
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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.