错误“不可订阅”调用函数时
我有下面的函数,它复制目录中的文件并在调用该函数的目录中重新创建它。当我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白如何使用此代码获得
'type' object is not subscriptable
(事实上,我可以在我的计算机上成功运行它并让它复制一个文件)。这表明您正在运行的代码不是您认为正在运行的代码。
我会做两件事:
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:
ipython
(to eliminate the possibility that you're accidentally calling an older version offind()
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
).在获取异常后立即使用
,可以准确定位哪个文件中的哪一行代码触发了错误,以及下标的
type
变量是什么。use
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.