Toby King
Toby King

Reputation: 129

Linux Server/Python: OSError: [Errno 13] Permission denied

I've tried writing a program in Python which takes some values, makes a directory and adds it to a text file for later use. I've uploaded it to an ubuntu VPS server as I intend to use it at a later date with my website. However, whenever I run the code (below) I get the following error:

Traceback (most recent call last):
  File "fileCreator.py", line 13, in <module>
    os.mkdir(dirName)
OSError: [Errno 13] Permission denied: 'just-a-test'

Python Code:

#!/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")

It's some sort of permission error and I've seen a few other topics on it but none of them have resulted in a solution. I'm not overly proficient with Linux commands, so bare with! Would really appreciate the help!

Upvotes: 1

Views: 20816

Answers (2)

Sascha Gottfried
Sascha Gottfried

Reputation: 3329

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

Upvotes: 0

bikemule
bikemule

Reputation: 325

TLDR; run chmod 744 in the directory with your Python script.

You don't have the correct permissions for the directory in which you are attempting to create the folder. From the same directory where you have fileCreator.py, run ls -la . on the command line and it will output something like this:

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

And probably a bunch of other files. The first line is the current directory. user is your login and staff is the group that owns it. They will be different on your system. The drwxr-xr-x is the permissions, and they are changed by the chmod command.

Check out more about Linux permissions here: https://www.linux.com/learn/understanding-linux-file-permissions

Upvotes: 4

Related Questions