- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
你好,
我想打开目录中具有特定名称的 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/
我正在处理的代码抛出错误 Unsupported operand type(s) for +: 'WindowsPath' and 'str' .我尝试了很多东西,但没有一个能解决这个问题(除了删除带
redpath = os.path.realpath('.') thispath = os.path.realpath(redpath) fspec = glo
我正在使用 pathlib 使用 Path 类管理我的 Python 项目中的路径。 当我使用 Linux 时,一切正常。但是在 Windows 上,我有一个小问题。 在我的代码中的某个时刻,我必须编
我想将大约 250 张图像及其文件名插入到 docx 文件中。 我的test.py文件: from pathlib import Path import docx from docx.shared i
我使用pip安装了spacy,然后使用下载了英文模型 $ python -m spacy 下载 en下载后给了我消息 您现在可以通过 spacy.load('en') 加载模型 使用 IPython,
这个问题在这里已经有了答案: Getting a typeError error in django project (3 个回答) 12 个月前关闭。 每当我运行服务器或在终端中执行任何命令时,都会
我编写了一个简单的序列化函数来存储文件树表示(基于复合模式...),但总是出现以下错误: java.io.NotSerializableException: sun.nio.fs.Wi
我正在跟进一个 django 教程,我刚刚使用 pip install django=2.1 安装了 django,它已成功安装,然后我使用 django-admin startproject App
你好, 我想打开目录中具有特定名称的 pdf 文件。这些文件名是从第二列中的 csv 文件输入中提供的。 我尝试了下面的代码,但是我收到了一条错误信息: TypeError: argument of
您好,我是 python 和 django 的新手,我正在练习 django,但是当我命令 python manage.py makemigration 和 python manage.py migr
我是 Pytest 的新手,一般都在开发。我正在尝试运行一个简单的 pytest 断言,但是收到错误: KeyError: WindowsPath('D:/ireact-tester-if/Lib/s
我是一名优秀的程序员,十分优秀!