Home > database >  How to use a variable instead of a constant in the following statement in python with PyPDF2
How to use a variable instead of a constant in the following statement in python with PyPDF2

Time:08-01

object = PyPDF2.PdfFileReader(r"C:\PROYECTOS\PruebasPDF\ArchivosPDF\326581098.pdf")

changing the path for a variable, how would the sentence be

path = "C:\PROYECTOS\PruebasPDF\ArchivosPDF\326581098.pdf"

object = PyPDF2.PdfFileReader(r....)

CodePudding user response:

you should be able to use

path = r"C:\PROYECTOS\PruebasPDF\ArchivosPDF\326581098.pdf"
object = PyPDF2.PdfFileReader(path)

The reason why the leading r is necessary are the \ symbols: In a normal string (without leading r) they are interpreted in a special way (for example "\t" is a string with a single tabulator, while r"\t" is a string with a \ and a t.

  • Related