Skip to content
Advertisement

Provide a file path or directory that contains files

I want to iterate through directories to find PDF files but I also want to be able to profile the file path without iterating through directories. So this is what I wrote:

rootdir = "C:\Users\user\Downloads\Final.pdf"

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        path = os.path.join(subdir, file)
        safe_text = ""
        print(path)
        with pdfplumber.open(str(path)) as pdf:
            print("this is a PDF file")

Advertisement

Answer

Use os.path.isdir/os.path.isfile to check if the path is a valid directory/file. If it’s a directory – walk it with a for loop, otherwise process it as file. It may help to extract the logic to a function so you don’t have to repeat yourself.

import os.path


def process(path):
    """Process a single PDF file."""
    safe_text = ""
    print(path)
    with pdfplumber.open(str(path)) as pdf:
        print("this is a PDF file")


path = "C:\Users\user\Downloads\Final.pdf"

if os.path.isdir(path):
    # it's a directory - walk it
    for subdir, dirs, files in os.walk(path):
        for file in files:
            subpath = os.path.join(subdir, file)
            process(subpath)
elif os.path.isfile(path):
    # it's a single file - process it
    process(path)
else:
    raise FileNotFoundError(path)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement