【问题标题】:FileNotFoundError: [Errno 2] No such file or directory: '1.pdf'FileNotFoundError:[Errno 2] 没有这样的文件或目录:'1.pdf'
【发布时间】:2021-04-15 03:44:08
【问题描述】:

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

我使用 # 来保持代码干净,不要介意!

# <------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

祝你有美好的一天!,

【问题讨论】:

  • 你应该在这里发布你的代码
  • 您是否尝试过提供 PdfFileMerger 的绝对路径,而不是相对路径?
  • 我只用那个

标签: python


【解决方案1】:

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

【讨论】:

  • manager = PdfFileMerger()
    对于 listdir(filedirinput) 中的文件:
    if files.endswith(pdf):
    manager.append(filedirinput, 'rb')跨度>
  • filedirinput 将只有路径没有文件名,files 参数将只有文件名没有路径信息。请打印这些变量的值以便更好地理解。您需要附加这两个以形成包含文件名的绝对路径
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 2021-08-24
  • 2021-03-07
  • 2015-06-09
  • 2021-04-01
  • 2021-10-15
  • 1970-01-01
相关资源
最近更新 更多