Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I get OSError: [Errno 13] Permission denied: <dir name>, and os.walk exits

Tags:

python

os.walk

I have a script to report me about all files, in a directory, so that users will be required to erase them (it's a really badly managed cluster, w/o real superuser). When I run the script I get: OSError: [Errno 13] Permission denied: ' ls: : Permission denied I can't write the dir name (company policy) The code is:

#!/depot/Python-3.1.1/bin/python3.1
from stat import *
import stat
import sys
from collections import defaultdict
from pwd import getpwuid
import sys
sys.path.append('/remote/us01home15/ldagan/python')
import mailer
import os
import re
import glob
import subprocess
import pwd
def find_owner(file):
    return pwd.getpwuid(os.stat(file)[stat.ST_UID]).pw_name
if (len(sys.argv) < 1):
    sys.error('''Please input <runda number> <case number>''')
files_by_users=defaultdict(list)
runda_num="".join(sys.argv[1])
dir_basic='/berry/secure' 
case_num="".join(sys.argv[2])
secure_dir="".join([dir_basic,"/"])
i=1
dirs=[]
runda_case_dir="".join([dir_basic,'/',runda_num,'/',case_num ])
while (os.path.exists(secure_dir)):
    if (os.path.exists(runda_case_dir)):
        dirs.append(runda_case_dir)
    i+=1
    secure_dir="".join([dir_basic,str(i)])
    runda_dir="/".join([secure_dir,runda_num,case_num])

#now finding list of 
manager_email='[email protected] [email protected]'
def bull (msg):
    i=1


for dir in dirs:
    for root,dirs,files in os.walk(dir,onerror=bull):
        for file in files:
            file_full_name=os.path.join(root,file)
            files_by_users[find_owner(file_full_name)].append(file_full_name)
for username in files_by_users:
        sendOffendingNotice(username, file_by+users[username], manager_email)

def sendOffendingNotice(username,filenames,managerEmail):
    """gets file name & manager Email
        sends an Email to the manager for review. As there are no smtp
        definitions, mailx shall be used"""
    user_email=username+'@synopsys.com'
    message="""The following files \n"""  + """\n""".join(filenames) +"""\n""" + \
    """ which belongs to user """ + username +""" does not meet the required names's SPEC\nPlease keep it under a directory which has a proper case/star name\n"""
    message= """echo \"""" + message+ """" | mailx -s "Offending files" """ + managerEmail +" " #+user_email
    process=subprocess.Popen(message,shell=True)

The script does not send the Email, but dies. Thanks for helping a newbe.

like image 390
Lior Avatar asked Sep 07 '10 16:09

Lior


People also ask

How do I fix errno 13 Permission denied windows?

The PermissionError: [errno 13] permission denied error occurs when you try to access a file from Python without having the necessary permissions. To fix this error, use the chmod or chown command to change the permissions of the file so that the right user and/or group can access the file.

How do I fix permission is denied in Python?

Check file path One of the main causes of PermissionError: [Errno 13] Permission denied is because Python is trying to open a folder as a file. Double-check the location of where you want to open the file and ensure there isn't a folder that exists with the same name. Run the os. path.

What is error No 13 in Python?

Your user don't have the right permissions to read the file, since you used open() without specifying a mode.


1 Answers

It sounds like your script is running as a normal user, and does not have permission to read a directory.

It would help to see the full error message (even if path names are changed), since it would tell us on which line the error was occurring.

But basically the solution is to trap the exception in a try...except block:

try:
    # Put the line that causes the exception here
    # Do not trap more lines than you need to.
    ...
except OSError as err:
    # handle error (see below)
    print(err)

Especially in light of S.Lott's comment, note that the files or directories which are causing OSErrors might be precisely the files whose owners you need to send email to. But in order to read inside their directories your script may need to run with superuser (or heightened) privileges.

like image 109
unutbu Avatar answered Sep 18 '22 11:09

unutbu