简体   繁体   English

调用函数时出现错误“无法下标”

[英]Error 'not subscriptable' when calling the function

I have the function below that copies a file in a directory and recreate it in the directory where the function is called. 我下面的函数可以将文件复制到目录中,然后在调用该函数的目录中重新创建该文件。 When I am running the code part by part in ipython, it is working fine. 当我在ipython中逐部分运行代码时,它工作正常。 However, when I execute it as a function it is giving me the following error: 但是,当我将其作为函数执行时,出现以下错误:

---> 17     shutil.copy2(filein[0], os.path.join(dir,'template.in'))

TypeError: 'type' object is not subscriptable

Here is the function 这是功能

import os
import shutil
from find import find

def recreatefiles(filedir):
    currdir = os.getcwd() # get current directory
    dirname = 'maindir'
    dir = os.path.join(currdir,dirname)
    if not os.path.exists(dir):
        os.makedirs(dir)

    #Copy .in files and create a template
    filein = find('*.in',filedir) # find is a function created

    shutil.copy2(filein[0], os.path.join(dir,'template.in'))

Any ideas about the error? 关于错误的任何想法? Thanks 谢谢

EDIT: Here is the code for find 编辑:这是查找代码

import os, fnmatch
def find(pattern, path):
    result = []
    for root, dirs, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                if not name.startswith('.'):
                    result.append(os.path.join(root, name))
    return result

EDIT2: Output of filein from ipython EDIT2:从ipython输出filein

  [1]: filein
  [2]: ['/home/Projects/test.in']

Basically, there is just one file. 基本上只有一个文件。 I used filein[0] in shutil.copy2 to remove the square brackets 我在shutil.copy2中使用了filein [0]来删除方括号

I don't see how you can possibly get 'type' object is not subscriptable with this code (as a matter of fact, I can successfully run it on my computer and have it copy one file). 我看不出如何用此代码'type' object is not subscriptable (事实上​​,我可以在计算机上成功运行它并复制一个文件)。

This suggests that the code you're running isn't the code that you think you're running. 这表明您正在运行的代码不是您认为正在运行的代码。

I would do two things: 我会做两件事:

  1. make sure that the code is exactly as it appears in your question; 确保代码与问题中的代码完全一致
  2. since you appear to be running this in an interactive shell, make sure you close and restart ipython (to eliminate the possibility that you're accidentally calling an older version of find() that got imported earlier). 由于您似乎是在交互式外壳中运行此程序,因此请确保关闭并重新启动ipython (以消除意外调用较早导入的较旧版本的find()的可能性)。

As a side note, I'd explicitly handle the case where filein is empty: the current code would raise an exception ( list index out of range ). 附带说明一下,我将明确处理filein为空的情况:当前代码将引发异常( list index out of range )。

use 采用

import pdb
pdb.pm()

just after getting the exception to be able to pinpoint exactly which line of code in which file triggers the error, and what variable is a type being subscripted. 在获得异常之后,就可以准确指出哪个文件的哪一行代码触发了错误,以及哪个变量是要下标的type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM