所以我用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
祝你今天愉快!
发布于 2021-04-15 03:52:25
您必须将完整(绝对)路径连同文件名传递到manager.append(文件)在line#12。
发布于 2022-12-03 23:57:54
@venkat的回答是正确的。manager.append(files)不包含完整路径,因此不能合并文件。
举个例子,试试
for files in listdir(filedirinput):
if files.endswith(pdf):
manager.append(filedirinput + str("\\") + files)
outputname = input("Please enter your desired filename: ")
merger.write(filedirinput + str("\\") + new_filename)
merger.close()https://stackoverflow.com/questions/67101966
复制相似问题