-1

(I am using Windows 10, PyCharm)

I am running the following script in the console:

pdfFileObj = open(samplepdf.pdf, 'rb')

This is the result:

NameError: name 'samplepdf' is not defined

I checked and made sure that the pdf file is within the project folder that I am operating out of on PyCharm.

I also tried running the same script with the direct path to the file:

pdfFileObj = open(C:\Users\Rolex\PycharmProjects\pdf\samplepdf.pdf, 'rb')

and received the same result:

NameError: name 'samplepdf' is not defined
David Makogon
  • 69,407
  • 21
  • 141
  • 189
Brand911
  • 3
  • 2
  • 1
    you need to make it a string, ie add double quotes around sample.pdf like `"sample.pdf"` – Albin Paul Feb 02 '23 at 14:14
  • Does this answer your question? [Why does using open(filename) fail with "filename not defined"?](https://stackoverflow.com/questions/57134908/why-does-using-openfilename-fail-with-filename-not-defined) – Gino Mempin Feb 02 '23 at 14:25

1 Answers1

0

Surround you filename with quotation marks

pdfFileObj = open('samplepdf.pdf', 'rb')

Whenever you get a '<> not defined', it means that python is treating <> as a variable. In this case, you do not have a variable called samplepdf. The error messages should help you diagnose your issues.

jjislam
  • 557
  • 3
  • 8