首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileNotFoundError:[Errno 2]没有这样的文件或目录:'1.pdf‘

FileNotFoundError:[Errno 2]没有这样的文件或目录:'1.pdf‘
EN

Stack Overflow用户
提问于 2021-04-15 03:44:08
回答 2查看 1.2K关注 0票数 0

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

我使用# thingy来保持代码的干净,不介意我这么做!

代码语言:javascript
复制
# <------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

祝你今天愉快!

EN

回答 2

Stack Overflow用户

发布于 2021-04-15 03:52:25

您必须将完整(绝对)路径连同文件名传递到manager.append(文件)在line#12。

票数 0
EN

Stack Overflow用户

发布于 2022-12-03 23:57:54

@venkat的回答是正确的。manager.append(files)不包含完整路径,因此不能合并文件。

举个例子,试试

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67101966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档