python - Python OS For Loop - TypeError:“模块”对象不可调用
问题描述
我有以下代码来遍历文件夹中的每个文件并在其pdfplumber
上运行模块:
#Import required packages:
import os
import pdfplumber
#Iterate over pdf's
os.chdir(r'C:\MyDocuments\PDF')
directory = r'C:\MyDocuments\PDF'
for filename in os.listdir(directory): #iterate over each file in directory
if filename.endswith(".pdf"): #if the file is a pdf (we will be creating .txt files - do not want to include these)
pdf = pdfplumber(filename) #pdfplumber extracts the pdf file first
else:
continue
使用 导入 PDF 后,还有更多事情要做pdfplumber
,但这个for
循环给出了问题的要点。当我运行它时,我收到错误消息
TypeError: 'module' object is not callable
似乎应该有一个解决方案,例如:from os import ...
where ... 应该是所需的任何功能(我认为)。如果这是一个解决方案,我不知道我需要从 os. 如果有人对如何使这项工作提出建议,我将不胜感激!
解决方案
pdfplumber
本身就是类名。您需要为此使用.open()
:
import os
import pdfplumber
#Iterate over pdf's
os.chdir(r'C:\MyDocuments\PDF')
directory = r'C:\MyDocuments\PDF'
for filename in os.listdir(directory): #iterate over each file in directory
if filename.endswith(".pdf"): #if the file is a pdf (we will be creating .txt files - do not want to include these)
pdf = pdfplumber.open(filename) #pdfplumber extracts the pdf file first
else:
continue
推荐阅读
- javascript - 使用 svg.js 时“未定义 SVG”
- python - decode 方法在 lambda env 中返回 unicode 响应
- regex - 重定向 ID 号为 htaccess 的 301 网址
- android - Ionic Bluetooth-Classic 插件,用于与本地到本地通信
- angular - Angular @ViewChild 错误“无法读取未定义的属性 nativeElement”
- angular - 'Unexpected token <' 在每个新版本的 Angular 生产 PWA 上,直到站点刷新
- sql - 什么相当于 Azure 中的 MySQL DELIMITER?
- rdma - RDMA 集群基准测试
- c++ - C++ 中初始化列表的顺序
- java - 从任务逐行更新 TextArea 的最佳方法是什么?