0

I want to create a simple text file in my Linux server using Python but I am not able to do so.

I created a directory in the server and the path is:

'var/createcode/code'

Inside the 'code' directory, I am storing my python script.

This is what I had tried so far:

code.py:

import pathlib

with open('data.txt', 'w') as text_file:
    text_file.write("This is a file")

Typically, a file called data.txt should be generated within the 'createcode' directory. However, I am receiving this error:

[Errno 13] Permission denied: 'data.txt'

What should I do? Where am I going wrong?

2 Answers 2

3

As the error message states, you (well, the user your script is running as) don't have permissions to create files in whatever the current working directory is.

Now before you start checking your directory permissions, you have to understand that the way it's currently written, your script will not create the file in "var/createcode" but in whatever the current working directory is at this moment - IOW if you launch your script from /home/someuser, it will try and create the file in /home/someuser.

To make sure your file is created where you expect it, you must specify an absolute path - any relative path will be "relative to the current working directory". This means you must either hardcode the absolute path (not very practical), derive it from some environment variable, or (in your case) derive it from the script's directory, using the __file__ magic variable:

import os

# the directory were the script is (ie /var/createcode/code)
here = os.path.abspath(os.path.dirname(__file__))

# you say you want you file to be created in /var/createcode, so
# you want the parent directory
parent = os.path.dirname(_here)

# now we can build the path
filepath = os.path.join(parent, "data.txt")

print("trying to create {}".format(filepath))

with open(filepath, "w") as f:
   f.write("hello world")

At this point, if you still have permissions issues, you'll have to check the permissions for the user under which your process is running - at least you know your checking the permissions for the correct directory ;-)

Sign up to request clarification or add additional context in comments.

2 Comments

Will this work regardless of any directory I am in? What if I change to 'home/user/code' or anything else? Will it still work?
@danny_boy the __file__ magic var contains your module / script path, so with this solution (which is very standard) it's garanteed that the filepath will be built relatively to where your module or script is - that's actually the whole point of this code snippet. Whether you're script will actually be able to create a file at this path is another question which answers depends on permissions, whether the filesystem is writable at all (some mount points can be read-only) and whatnots...
0

That's user permission troubles. Your user which run this python script don't have permission to write to directory. Try to write to another dir or change dir permissions with chmod and chown commands.

sudo chmod 666 /var/createcode

sudo chown %YOURUSERNAME% /var/www/createcode

Note that first command will allow writing to /var/www/createcode from any user.

Hope that helps.

3 Comments

I have done that already actually. I have used: chown :www-data createcode/ and chmod -R 775 createcode
yep, you made www-data group as owner of directory and grant write, read, exec for owner and group, and read-exec for anyone in system for directory and all files in this dir recursively. Grats)
But it still doesnt work. I am receiving the same error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.