gpt4 book ai didi

python - 类型错误 : argument of type 'WindowsPath' is not iterable - in open of pdf file with python

转载 作者:太空宇宙 更新时间:2023-11-04 05:34:01 27 4
gpt4 key购买 nike

你好,

我想打开目录中具有特定名称的 pdf 文件。这些文件名是从第二列中的 csv 文件输入中提供的。

我尝试了下面的代码,但是我收到了一条错误信息:

TypeError: argument of type 'WindowsPath' is not iterable

如何解决这个问题以及要打开的pdf文件 根据输入文件?

还有一个问题:如果 输入名称与 pdf 标题不完全匹配,但我仍然想要 打开这个包含输入名称的文件?

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()

提前致谢!

最佳答案

if file_name in pdf_file 行中的问题:pdf_file 不是字符串,而是 pathlib.Path 的实例,请使用 name以字符串形式获取文件名:

if file_name == pdf_file.name

如果您想检查 file_name 没有 ext 是否包含在 pdf_file name 中:

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

关于python - 类型错误 : argument of type 'WindowsPath' is not iterable - in open of pdf file with python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36202250/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com