I want to create a file and open it and write on it with f= open('out.txt', 'a')

then it got error:
File "m.py", line 10, in <module>
f=open('./out.txt', 'a')
IOError: [Errno 13] Permission denied: './out.txt'

What happened??


I am using opensuse and was using a user account to edit and run the m.py file
python2.6.4 version

from BeautifulSoup import BeautifulSoup
import re
import urllib2

file = urllib2.urlopen("http://www.tripadvisor.com/ShowUserReviews-g294217-d305813-r49043831-Langham_Place_Hong_Kong-Hong_Kong_Hong_Kong_Region.html#REVIEWS")

soup = BeautifulSoup(file)
tag=soup.findAll(id=re.compile("^review"))

f=open('out.txt', 'a')


str = tag[1].contents
f.write(str)

Dani AI

Generated

The Python IOError 13 ("Permission denied") is an OS-level refusal to open or create the target path. On Linux that usually means the user running the script does not have write permission to the directory or file, or something else at the filesystem/security layer is blocking writes. As suggested, start by checking permissions; later confirmed the directory was owned by root, which explains the failure.

Quick diagnostic checklist (run as the account that executes the script):

id
ls -ld .
ls -l out.txt          # if the file already exists
getfacl .               # check ACLs, if available
lsattr .                # check immutable attribute
df -h .                 # ensure filesystem is writable and has space

If the directory is owned by another account (root in this case), fix either ownership or group permissions so the script user can write there. Prefer changing the directory owner or group (and adding your user to that group) over making the directory world-writeable. If nothing in permissions explains it, check SELinux/AppArmor logs for denials or filesystem mount options for read-only status.

A few Python-specific tips to avoid further problems: don’t shadow builtins by naming variables file or str; convert BeautifulSoup nodes to a proper byte string before writing; and use a context manager so files are closed automatically. For example:

content = unicode(tag[1]).encode('utf-8')
with open('/absolute/path/to/out.txt', 'a') as f:
    f.write(content)

Avoid running development scripts as root unless absolutely necessary. Changing ownership/permissions once to a sensible, secure setup will prevent recurring errors and is safer than developing under the root account.

Recommended Answers

All 6 Replies

What platform is this? Windows XP, Ubuntu Linux, Mac OSX, etc?

There's a number of possibilities here. Most likely the folder you're working in doesn't give you write permissions.

I use opensuse

I use opensuse

Ok, so it's most likely that the folder you're trying to open the file in doesn't have write permissions for the user that is running the python script. You could either run the script as root or simply write the file to your home directory with ~/ instead of ./

how can I change the permission setting?
I have the root account password.
I tried your method, now I can write in home directory, but not others.

how can I change the permission setting?
I have the root account password.
I tried your method, now I can write in home directory, but not others.

You would use chmod with the target being the directory

That or simply login as root before executing the script.

Oh!
I found out that the creator of the folder is root!!
because I was using root account that time, so user of course cannot modify it.....silly me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.