通过变量将列表元素传递给函数

时间:2018-10-26 04:21:18

标签: python-3.x

不知道为什么,但是代码出错了,但是当我在函数“ apply_patches”中传递实际元素时,它可以正常工作。 根据我的理解,元素变量“ item”是根本原因。

import re
import subprocess
from plumbum import local, cmd

osTyp = cmd.uname('-s').strip()

def apply_patches(msg, patch, zipfile, dir):
    print(msg)
    for j in patch:
        if osTyp == 'Linux':
            local.cwd.chdir('/u02/')
            cmd.unzip('-o', zipfile.format(j))
            local.cwd.chdir(dir.format(j))
            cmd.opatch('apply', '-silent')
            print('Patch' ,j, 'applied')
        elif osTyp == 'SunOs':
            cmd.unzip("p{}_121020_Solaris-x86-64.zip".format(j))
        else:
            print("!!\n!! unable to determine OS type !!\n!!")


s = subprocess.check_output(["opatch", "lsinventory"])
output = s.decode("utf-8")


patches = [27923320, 27547329, 21171382, 21463894, 18961555, 28432129]

patches_found = set(re.findall(r'\b(?:%s)\b' % '|'.join(map(str, patches)), output))
patches_missing = set(map(str, patches)) - patches_found

my_list = list(patches_missing)
print (my_list)
for item in my_list:
    if item in ['27923320', '27547329']:
        apply_patches('Applying PSU patches', item,"/u02/p28317232*.zip", "/u02/28317232/{}")
    else:
        apply_patches('Applying one-off patches', item, "/u02/p{}_*.zip", "/u02/{}")

错误:

['27923320', '28432129', '21171382', '27547329', '18961555', '21463894']
Applying PSU patches
Traceback (most recent call last):
  File "/u02/scripts/Patching/Test.py", line 37, in <module>
    apply_patches('Applying PSU patches', item,"/u02/p28317232*.zip", "/u02/28317232/{}")
  File "/u02/scripts/Patching/Test.py", line 15, in apply_patches
    local.cwd.chdir(dir.format(j))
  File "/usr/local/lib/python3.6/site-packages/plumbum/path/local.py", line 354, in chdir
    os.chdir(str(newdir))
FileNotFoundError: [Errno 2] No such file or directory: '/u02/28317232/2'

Process finished with exit code 1

0 个答案:

没有答案