0

Using from PyPDF2.pdf import PdfFileReader

My temp = open('HW8.pdf', 'rb') returns an error when run, saying HW8.pdf could not be found even though it's in the same folder as my python code.

Not sure how to go from here.

grantiod
  • 19
  • 4

1 Answers1

0

Make sure you are in the correct directory; I tried your snippet, and realized that I wasn't in the correct directory. The 'test.pdf' file was on my desktop, and this didn't throw any errors:

import os
    
from PyPDF2 import PdfFileReader
os.chdir('/home/asylumax/Desktop')    
temp = open('test.pdf', 'rb')
asylumax
  • 781
  • 1
  • 8
  • 34
  • I implemented ```os.chdir("C:\Users\giodi\invoice-generator")``` and it still returns an error, saying **SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape** – grantiod Dec 18 '21 at 02:15
  • Sounds like the PDF might be damaged or a bit odd. Create a simple document, save as a PDF, and try that. This is different than the error you originally got, so that's a start. – asylumax Dec 18 '21 at 02:17
  • Same thing. Made test.txt and then saved it as a test.pdf. Same error occurred. – grantiod Dec 18 '21 at 02:18
  • I tried this pdf: https://s1.q4cdn.com/806093406/files/doc_downloads/test.pdf and it worked. See if you can read it. – asylumax Dec 18 '21 at 02:20
  • Same problem, same error. – grantiod Dec 18 '21 at 02:22
  • Check the error; I searched on it, and it could be you need r"...."; see: https://stackoverflow.com/questions/37400974/unicode-error-unicodeescape-codec-cant-decode-bytes-in-position-2-3-trunca like os.chdir(r"C:\Users\giodi\invoice-generator") – asylumax Dec 18 '21 at 02:23
  • New error when I put ```os.chdir(r"C:\Users\giodi\invoice-generator")``` **File "C:\Users\giodi\AppData\Local\Programs\Python\Python39\lib\site-packages\gtts\tts.py", line 313, in save self.write_to_fp(f) File "C:\Users\giodi\AppData\Local\Programs\Python\Python39\lib\site-packages\gtts\tts.py", line 259, in write_to_fp prepared_requests = self._prepare_requests() File "C:\Users\giodi\AppData\Local\Programs\Python\Python39\lib\site-packages\gtts\tts.py", line 201, in _prepare_requests text_pa ...(ran out of space here)... AttributeError: 'PageObject' object has no attribute 'strip'** – grantiod Dec 18 '21 at 02:28
  • Are you doing this from a command line, or from an IDE like Spyder? Try from a command line. This should be a straightforward read, and we know the file is good. So, something about your implementation is off. Make sure you are in that directory, for sure, with print("pwd:"+os.getcwd()) – asylumax Dec 18 '21 at 02:30