[英]Python loop over files in directory as function argument
In my python scirpt I have a function func01
that takes a pdf file as argument: 在我的python脚本中,我有一个函数
func01
,它使用一个pdf文件作为参数:
func01("pdf1.pdf")
This works and gives me the expected output. 这有效,并给了我预期的输出。
Now I want to perform func01
on all files in my directory. 现在,我想对目录中的所有文件执行
func01
。 I tried this: 我尝试了这个:
import glob, os
os.chdir("/articles")
for file in glob.glob("*.pdf"):
myoutput = func01(file)
... but this does not work: TypeError: 'str' object is not callable
...但是这不起作用:
TypeError: 'str' object is not callable
Searching on SO, I re-wrote my script to: 搜索SO,我将脚本重新编写为:
for file in glob.glob("*.pdf"):
path_ = os.path.realpath(file)
myputput = func01(path_)
So what I want is to use each file from the loop as input for my function. 所以我想要的是使用循环中的每个文件作为我的函数的输入。 What am I missing in my code?
我的代码中缺少什么?
Your error message 您的错误讯息
TypeError: 'str' object is not callable
TypeError:“ str”对象不可调用
implies that somewhere along the line you're doing "some string"(...)
. 暗示您正在沿着某处执行
"some string"(...)
。 In the code you posted you only have a few function calls. 在发布的代码中,您只有几个函数调用。
os.chdir("/articles")
glob.glob("*.pdf")
myoutput = func01(file)
So either os.chdir
, glob.glob
, or func01
have been overwritten to be a string. 因此,
os.chdir
, glob.glob
或func01
已被覆盖为字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.