2

I am getting stuck when trying to iterate through files in a directory ('PDFS') with fitz from PyMuPDF. The thing is, the code works when I am just doing document = "somepdf.pdf", but as soon as I insert a for loop and try to access files that way this error shows up:

filename, stream, filetype, rect, width, height, fontsize RuntimeError: cannot open sample.pdf: No such file or directory

Here is the code:

for file in os.listdir('PDFS'):
        if fnmatch.fnmatch(file, '*.pdf'):
            document = file
            doc = fitz.open(document)

Thank you for the help!

2 Answers2

2

Your pdf files to open is under sub-directory PDFS, e.g. PDFS/sample.pdf, while your code fitz.open(document) is to open file under current working directory. So, a fix should be:

import fitz
import os
import fnmatch

for file in os.listdir('PDFS'):
    if fnmatch.fnmatch(file, '*.pdf'):
        document = os.path.join('PDFS', file)
        doc = fitz.open(document)

Furthermore, a relative path PDFS is used, so you have to run the code right under the path where PDFS in, say /your/workspace/:

/your/workspace > python code.py

Otherwise,

/your > python workspace/code.py

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'PDFS'

So, a good practice is to

  • use full path if PDFS is just a user input path; otherwise,

  • use relative path to the script path

    script_path = os.path.abspath(__file__)
    project_path = os.path.dirname(script_path)
    pdfs_path = os.path.join(project_path, 'PDFS')
    
skyway
  • 171
  • 1
  • 7
2

I had the same issue. Its because PyCharm automatically installs the module named fitz whereas you need to write the following in the terminal:

pip install PyMuPDF

This will automatically install fitz and you can use the function fitz.open()

Hassan Shahzad
  • 455
  • 6
  • 14