IF条件下索引超出范围

时间:2019-12-19 15:55:30

标签: python python-2.7

import subprocess

print "# Enter the change number, and hit enter \n## Example: chg12345\n\n "

changeNum  =   raw_input();
fileName = changeNum+"_groupcreate.cmd"
outFile = open(fileName,'w')
print "## Cut and paste usernames from the spreadsheet.\n## End with CTRL-D\n"

msg = sys.stdin.readlines()
#file = open(fileName, "w")

for item in msg:
        group = item.split()[0]

        if (item.split()[1]) is None:
            dn = "ou=groups,ou=security,o=MasterCard WorldWide,dc=mastercard,dc=com"

        output = "group create "+group+" \""+dn+"\""+" "+group+"\n"
        try:
                print "iteration\n"
                outFile = open(fileName,'a')
                outFile.write(output)
                #outFile.close()
        except IOError as (errno,strerror):
                print "I/O error({0}): {1}".format(errno, strerror)

在这里,我想查看item.split()[1]是否为null,如果为null,那么我想为 dn 变量提供默认值,否则为 dn 变量将与item.split()[1]的值一起转储。但是当我执行它时,我超出了范围错误。

输入: see the below image

输出:

Traceback (most recent call last):   File "y.py", line 19, in <module>
    if `(item.split()[1])` is None: IndexError: list index out of range

0 个答案:

没有答案