在遇到Python错误后继续循环

时间:2018-03-15 18:16:18

标签: python error-handling synology

这个问题与其他有关异常处理的问题略有不同。

我正在尝试使用名为SynoACL的Python模块为Synology NAS上的文件夹设置权限,该模块包含Synology NASes中包含的(其他文档记录不完整的)SynoACLTool:https://github.com/zub2/synoacl

我的代码获取目录中所有子目录的列表。子目录都以AD中的现有用户命名,我试图给予他们对共享其名称的文件夹的修改权限(它们将成为网络上的用户共享)。该脚本会引发错误,但它实际上已成功设置了权限,尽管存在错误。问题是它只对目录中的第一个子目录执行此操作,并且由于该错误而不会继续执行任何其他子目录。我已经尝试将其设置为忽略所有异常,但它仍然失败。这是我的代码和错误,删除了个人信息:

错误:

    Traceback (most recent call last):
  File "synologyusershareperms.py", line 19, in <module>
    SynoACLTool.add(".", SynoACL.fromString(folderPermString))
  File "/volume1/@appstore/py3k/usr/local/lib/python3.5/site-packages/synoacl/tool.py", line 406, in add
    return SynoACLTool._parseACLResult(SynoACLTool._communicate(["-add", path, str(acl)]))
  File "/volume1/@appstore/py3k/usr/local/lib/python3.5/site-packages/synoacl/tool.py", line 370, in _communicate
    return subprocess.check_output([ SynoACLTool._SYNOACL_CMD ] + args, universal_newlines = True).split("\n")
  File "/volume1/@appstore/py3k/usr/local/lib/python3.5/subprocess.py", line 629, in check_output
    **kwargs).stdout
  File "/volume1/@appstore/py3k/usr/local/lib/python3.5/subprocess.py", line 711, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['synoacltool', '-add', '.', 'user:DOMAINNAME\\username:allow:rwxpd-aARWc--:fd--']' returned non-zero exit status 255

代码:

import os
from synoacl.tool import SynoACL, SynoACLTool
user_share_path = '/path/to/share/'
permTemplate1 = "user:DOMAINNAME\\"
permTemplate2 = ":allow:rwxpd-aARWc--:fd--"

os.chdir(user_share_path)
cwd = os.getcwd()
print(cwd)

folderList = os.listdir()
try:
    for folderName in folderList:
        os.chdir(folderName)
        cwd = os.getcwd()
        print(cwd)
        folderPermString = permTemplate1 + folderName + permTemplate2
        print(folderPermString)
        SynoACLTool.add(".", SynoACL.fromString(folderPermString))
        os.chdir("..")
except:
    pass

由于添加了try和except行,它不会再抛出任何错误,但它会在第一个循环后停止。有没有办法让它继续执行循环,尽管有错误?

编辑:评论建议将try放入for循环中,似乎没有修复它:

for folderName in folderList:
    try:
        os.chdir(folderName)
        cwd = os.getcwd()
        print(cwd)
        folderPermString = permTemplate1 + folderName + permTemplate2
        print(folderPermString)
        SynoACLTool.add(".", SynoACL.fromString(folderPermString))
        os.chdir("..")
    except:
        pass

2 个答案:

答案 0 :(得分:0)

将try / except块放在for循环中:

for folderName in folderList:
    try:
        os.chdir(folderName)
        cwd = os.getcwd()
        print(cwd)
        folderPermString = permTemplate1 + folderName + permTemplate2
        print(folderPermString)
        SynoACLTool.add(".", SynoACL.fromString(folderPermString))
        os.chdir("..")
    except:
        pass

你也可以这样做(如果你不想做任何关于捕获异常的事情)

# contextlib module
from contextlib import suppress

with suppress(Exception):
    os.chdir(folderName)
    cwd = os.getcwd()
    print(cwd)
    folderPermString = permTemplate1 + folderName + permTemplate2
    print(folderPermString)
    SynoACLTool.add(".", SynoACL.fromString(folderPermString))
    os.chdir("..")

答案 1 :(得分:0)

替换此行

except:

通过

except Exception as ex:

所以:

import os
from builtins import Exception
user_share_path = '/path/to/share/'
permTemplate1 = "user:DOMAINNAME\\"
permTemplate2 = ":allow:rwxpd-aARWc--:fd--"

os.chdir(user_share_path)
cwd = os.getcwd()
print(cwd)

folderList = os.listdir()

for folderName in folderList:
   try:
       os.chdir(folderName)
       cwd = os.getcwd()
       print(cwd)
       folderPermString = permTemplate1 + folderName + permTemplate2
       print(folderPermString)
       SynoACLTool.add(".", SynoACL.fromString(folderPermString))
       os.chdir("..")
    except Exception as ex:
      print(ex)
      pass