错误“不可订阅”调用函数时

发布于 2024-12-28 04:25:41 字数 1220 浏览 2 评论 0原文

我有下面的函数,它复制目录中的文件并在调用该函数的目录中重新创建它。当我在 ipython 中部分运行代码时,它工作正常。但是,当我将它作为函数执行时,它会给出以下错误:

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

TypeError: 'type' object is not subscriptable

这是函数

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'))

关于该错误有什么想法吗?谢谢

编辑:这是 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

编辑2:ipython 的 filein 输出

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

基本上,只有一个文件。我在shutil.copy2中使用filein[0]删除方括号

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. 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

  [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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱人配狗天长地久 2025-01-04 04:25:41

我不明白如何使用此代码获得 'type' object is not subscriptable (事实上,我可以在我的计算机上成功运行它并让它复制一个文件)。

这表明您正在运行的代码不是您认为正在运行的代码。

我会做两件事:

  1. 确保代码与您的问题中出现的完全一致;
  2. 由于您似乎是在交互式 shell 中运行此命令,因此请确保关闭并重新启动 ipython(以消除您意外调用旧版本的 find() 的可能性) > 之前导入的)。

作为旁注,我将明确处理 filein 为空的情况:当前代码将引发异常(列表索引超出范围)。

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).

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).

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).

少钕鈤記 2025-01-04 04:25:41

在获取异常后立即使用

import pdb
pdb.pm()

,可以准确定位哪个文件中的哪一行代码触发了错误,以及下标的 type 变量是什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文