简体   繁体   中英

Python OS For Loop - TypeError: 'module' object is not callable

I have the following code to iterate over each file in a folder and run the pdfplumber module on it:

#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

There is more going on once the PDF is imported with pdfplumber , but this for loop gives the gist of the issue. When I run this, I get the error message

TypeError: 'module' object is not callable

It seems that there should be a solution in something like: from os import ... where ... should be whatever function is needed (I think). If this is a solution though, I don't know which specific function I would need to import from os. If someone would have a suggestion on how to make this work, I would greatly appreciate it!

pdfplumber is itself the class name. You need to use .open() for this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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