Python错误:字符串索引必须是整数,而不是str

时间:2014-07-25 18:11:43

标签: python parsing for-loop types indices

好吧,我有一个明显的问题,盯着我,我无法弄明白。我得到了我需要的输出/结果但我得到了TypeError:"字符串索引必须是整数,而不是str"。以下是我的代码示例。这是因为声明" 如果f不在GetSquishySource(dirIn)"基本上我想查看特定文件是否在另一个列表中,以便我不会将其添加到我正在创建的zip文件中。我只是没有看到这里的问题以及如何解决它。任何帮助将不胜感激。

def compressLists(z, dirIn, dirsIn, filesIn, encrypt=None):
    try:
        with zipfile.ZipFile(z, 'w', compression=zipfile.ZIP_DEFLATED) as zip:
            # Add files
            compressFileList(z, dirIn, dirIn, filesIn, zip, encrypt)

            # Add directories
            for dir in dirsIn:
                dirPath = os.path.join(dirIn, dir["name"])
                for root, dirs, files in os.walk(dirPath):
                    # Ignore hidden files and directories
                    files = [f for f in files if not f[0] == '.']
                    dirs[:] = [d for d in dirs if not d[0] == '.']

                    # Replace file entries with structure value entries
                    for i, f in enumerate(files):
                        del files[i]
                        **if f not in GetSquishySource(dirIn):**
                            files.insert(i, {'zDir': dir["zDir"], 'name': f})

                    compressFileList(z, dirIn, root, files, zip, encryptedLua)
                    if dir["recurse"] == False:
                        break;

以下是我创建并调用的GetSquishySource函数。

def GetSquishySource(srcDir):
    squishyLines = []
    srcToRemove = []
    if os.path.isfile(srcDir + os.path.sep + "squishy"):
        with open(srcDir + os.path.sep + "squishy") as squishyFile:
            squishyContent = squishyFile.readlines()
        squishyFile.close()

        for line in squishyContent:
            if line.startswith("Module") and line is not None:
                squishyLines.append(line.split(' '))

        for s in squishyLines:
            if len(s) == 3 and s is not None:
                # If the 3rd column in the squishy file contains data, use that.
                path = s[2].replace('Module "', '').replace('"', '').replace("\n", '')
                srcToRemove.append(os.path.basename(path))
            elif len(s) == 2 and s is not None:
                # If the 3rd column in the squishy file contains no data, then use the 2nd column.
                path = s[1].replace('Module "', '').replace('"', '').replace("\n", '').replace(".", os.path.sep) + ".lua"
                srcToRemove.append(os.path.basename(path))

    return srcToRemove

0 个答案:

没有答案