user2469733
user2469733

Reputation: 19

IO Error [Errno 2]

So I'm a beginning programmer, and python is my first language. I'm trying to write a script that will open a random PDF from a directory and select a random page from that PDF to read. When I run my script I get the error code IO ERROR: [Errno 2] and then displays the title of the selected PDF. How can I fix this? I am using the pyPdf module. Are there any other problems in the code you can see?

    import os, random, pyPdf

    from pyPdf import PdfFileReader

    b = random.choice(os.listdir("/home/illtic/PDF"))

    pdf_toread = pyPdf.PdfFileReader(open(b, 'r'))

    last_page = pdf_toread.getNumPages() - 1

    page_one = pdf_toread.getPage(random.randint(0, last_page))

    print " %d " % page_one

Upvotes: 1

Views: 824

Answers (1)

mawimawi
mawimawi

Reputation: 4333

what value does b have? I am pretty sure that it is just the filename without the path. Try adding the path in front of the filename and it should be ok.

pdf_toread = pyPdf.PdfFileReader(open('/home/illtic/PDF/' + b, 'r'))

Upvotes: 4

Related Questions