FileNotFoundError:[Errno 2]没有这样的文件或目录:“1.pdf”

2024-10-04 07:30:12 发布

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

因此,我使用Python进行PDF合并,因为我发现对于像我这样的初学者来说,这是一个很好的项目。我从使用PyPDF4开始,在完成了所有艰苦的工作(不是那么艰苦)之后,我运行了这个程序,结果只听到“FileNotFoundError:[Errno 2]没有这样的文件或目录:'1.pdf'”。 第一个问题是,它确实找到了指定目录中的文件名,并且确实存在。它是如何找到它的名字却仍然说它不存在的?第二个问题,我如何摆脱这个问题:<

我使用#thingy来保持代码干净,如果我这样做了,请不要介意

# <------Import Modules-------->
from PyPDF4 import PdfFileMerger
from os import listdir
# <-----------Misc------------->
filedirinput = input("Please enter a directory destination: ")
pdf = (".pdf")
# <-----Merge our Files------------------
manager = PdfFileMerger()# <------------|   PdfFileMerger is now "manager" so that Karens can call it anytime XD
for files in listdir(filedirinput):# <--|  For all the files in our Input Directory
    if files.endswith(pdf):# <-------| Check if file ends with .pdf and move to next step
        manager.append(files)# <--------| Merge all our files in the Directory using Manager (PdfFileMerger)
# <--------Output Time YE!!!--------->
outputname = input("Please enter your desired filename: ")
manager.write(outputname + pdf)
# <-----------Misc------------->
print(f"{outputname + pdf} was generated in {filedirinput}")

# NOTE This part is in development and you currently CANNOT mail somebody
# ALSO, I might turn all of this into a Tkinter GUI program :)

print("Do you want to email this to someone: Y/N")
yn = input("> ")
if yn == "N":
    print("Thank You for Using PyDF Merger :)")
    print("Made By NightMX!")

我收到一个错误:https://imgur.com/a/sXGpq7R

祝你今天愉快


Tags: toin目录inputifpdfmanagerour
1条回答
网友
1楼 · 发布于 2024-10-04 07:30:12

您必须将完整(绝对)路径和文件名一起传递给manager.append(files)在第12行。您在Ln#6处获得的目录用于检索文件列表,但是您在Ln#12处追加文件时未使用此目录

相关问题 更多 >