2010-09-07 40 views
9

我有一个脚本来报告我所有的文件,在目录中,以便用户将被要求删除它们(这是一个非常严重管理群集,没有真正的超级用户)。 当我运行该脚本,我得到: OSERROR:[错误13]许可被拒绝:“ LS:权限被拒绝 我不能写的目录名(公司政策) 的代码是:我得到OSERROR:[错误13]许可被拒绝:<dir name>和os.walk退出

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

该脚本不发送电子邮件,但死亡。 感谢您的帮助。

+2

您应该通过pep8脚本运行此操作并修复格式化... – Daenyth 2010-09-07 17:54:31

+1

无论如何,在提问时不要粘贴您的整个代码 - 删除不相关的内容,不要指望其他人通过不相关的代码进行操作。为了大家的理智,请不要在任何地方到处使用“”“长串”“”。 – 2010-09-07 20:03:55

回答

2

这听起来像你的脚本运行作为一个普通用户,并没有权限读取目录。

这将有助于看到完整的错误信息(即使路径名称更改),因为它会告诉我们这行错误发生在。

但基本的解决办法是陷阱在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) 

尤其是在美国洛特的评论来看,注意,这是造成OSErrors可能正是其拥有者的文件的文件或目录你需要发送电子邮件给。但为了阅读他们的目录,你的脚本可能需要以超级用户(或高级)权限运行。

+1

同样,使这是一个可能的http://superuser.com问题,因为它是关于权限,而不是Python。 – 2010-09-07 17:32:29

相关问题