Sorin Beginner
Sorin Beginner

Reputation: 91

TypeError: argument of type 'WindowsPath' is not iterable - in open of pdf file with python

Good day,

I want to open the pdf files that have a specific name from a directory . These file names are provided from a csv file input, which are in the second column.

I tried the follwing code, but I received an error message:

TypeError: argument of type 'WindowsPath' is not iterable

How can I solve this problem and the pdf files to be opened according the input file?

And another issue: how can I fix if the input name is not an exact match with the pdf title,but I still want to open this file that contain the input name?

import csv
import os
from pathlib import *

dir_path = Path('D:\\path\\pdf files')
pdf_files = dir_path.glob('*.pdf')

file1=open('INPUT.csv','r')
reader=csv.reader(file1,delimiter=';')
for pdf_file in pdf_files:
    for item in reader:
        file_name=item[1]
        print(file_name)#just to see the file name that I want to open 
        if file_name in pdf_file:
            os.startfile("%s"%(pdf_file))
file1.close()

Thank you in advance!

Upvotes: 3

Views: 28498

Answers (1)

Mikhail Gerasimov
Mikhail Gerasimov

Reputation: 39576

Problem in line if file_name in pdf_file: pdf_file is not string, but instance of pathlib.Path, use name to get file name as string:

if file_name == pdf_file.name

In case you want to check if file_name without ext contains in pdf_file name:

file_name.split('.')[-2] in f.name  # ('example' in 'some_example.pdf') == True

Upvotes: 4

Related Questions