简体   繁体   English

pyPdf无法写入文件吗?

[英]File writing is not working with pyPdf?

I am newer to python. 我是python的新手。 I was try open the pdf files and write its content into the new text files. 我尝试打开pdf文件,并将其内容写入新的文本文件。 That the text files name are generate by the pdf name. 文本文件名由pdf名称生成。 I tried so far but it is not give what i expect. 到目前为止,我已经尝试过了,但是并没有达到我的期望。 How can i achieve it 我该如何实现

    import glob, os
    import pyPdf
    os.chdir("pdf/")
    for file in glob.glob("*.pdf"):
            filena = file
            filename = "c:/documents/"+filena+".txt"
            target = open(filename,'w')
            pdf = pyPdf.PdfFileReader(open(filena,"rb"))
            for page in pdf.pages:
                target.write (page.extractText())
            target.close()

Results the Error 结果错误

File "c:/documents/atpkinase.pdf.txt",line 7, in <module>  
target = open(filename,'w')
IOError: [Errno 2] No such file or directory: "c:/documents/atpkinase.pdf.txt" 

Looks like if the directory "c:/documents/" does not exist. 看起来如果目录"c:/documents/"不存在。 To write file to it you must create directory first. 要向其中写入文件,必须首先创建目录。 To check directory existent (and create it if needed) you can use 要检查目录是否存在(并根据需要创建目录),可以使用

dir = "c:/documents"
if not os.path.exists(dir):
    os.makedirs(dir) 

Also, filea contains file name with extension, and when you create filename you need only a file name of old file without extension. 此外, filea包含带有扩展名的文件名,并且在创建filename您只需要没有扩展名的旧文件的文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM