2010-09-07 13 views
9

से बाहर निकलता है मेरे पास एक निर्देशिका में सभी फ़ाइलों के बारे में रिपोर्ट करने के लिए एक स्क्रिप्ट है, ताकि उपयोगकर्ताओं को उन्हें मिटाना होगा (यह वास्तव में बुरी तरह से है प्रबंधित क्लस्टर, डब्ल्यू/ओ असली सुपरसुर)। जब मैं स्क्रिप्ट चलाने मैं: OSError: [errno 13] अनुमति अस्वीकृत: ' ls:: अनुमति से इनकार मैं dir नाम (कंपनी की नीति) कोड है नहीं लिख सकते हैं:मुझे ओएसईआरआरआर मिलती है: [एर्र्नो 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

आपको इसे पीडी 8 स्क्रिप्ट के माध्यम से चलाया जाना चाहिए और स्वरूपण को ठीक करना चाहिए ... – Daenyth

+1

किसी भी प्रश्न पूछने पर अपना पूरा कोड पेस्ट न करें - अप्रासंगिक सामान को हटाएं, अन्य सभी को असंबंधित कोड से गुजरने की उम्मीद न करें। और कृपया, हर किसी की सैनिटी के लिए, किसी भी कारण से हर जगह "" लंबे तारों का उपयोग करना बंद करें। –

उत्तर

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) 
विशेष रूप से S.Lott की टिप्पणी के आलोक में

, ध्यान दें कि फाइल या निर्देशिका जो उत्पन्न कर रहे हैं OSErrors ठीक फ़ाइलों जिनके स्वामियों हो सकता है आपको ईमेल भेजने की जरूरत है। लेकिन अपनी निर्देशिकाओं के अंदर पढ़ने के लिए आपकी स्क्रिप्ट को सुपरसियर (या बढ़ते) विशेषाधिकारों के साथ चलाने की आवश्यकता हो सकती है।

+1

इसके अलावा, यह एक संभावित http://superuser.com प्रश्न बनाता है, क्योंकि यह अनुमतियों के बारे में है, पायथन नहीं। –

संबंधित मुद्दे