Python Pathlib path对象未转换为字符串

2024-06-26 14:54:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用shuil从Pathlib使用path对象复制pdf文件,但是当我运行代码时,我得到错误“str object is not callable” 当使用str()将路径转换回字符串时。任何解释为什么会发生这种情况都会很有帮助。谢谢!

from pathlib import Path
from wand.image import Image as wandImage
import shutil
import sys
import os

def pdf2Jpeg(pdf_path):
    pdf = pdf_path
    jpg = pdf[:-3] + "jpg"
    img = wandImage(filename=pdf)
    img.save(filename=jpg)

src0 = Path(r"G:\Well Schematics\Well Histories\Merged")
dst0 = Path(r"G:\Well Schematics\Well Histories\Out")
if not dst0.exists():
    dst0.mkdir()

pdfs = []
api = ''
name = ''
pnum = ''
imgs = []

for pdf in src0.iterdir():
    pdfs.append(pdf)

for pdf in pdfs:

    if not dst0.exists():
        dst0.mkdir()

    str = str(pdf.stem)
    split = str.split('_')
    api = split[0]
    name = split[1]
    pnum = split[2]

    shutil.copy(str(pdf), str(dst0))
    for file in dst0.iterdir():
        newpdf = file
    pdf2Jpeg(str(newpdf))
    newpdf.unlink()

Tags: pathinfromimportforpdfnotjpg