Pratik Jaiswal Pratik Jaiswal - 2 months ago 6
Python Question

Declare global variables and access locally

I have two assignments/variables in my code:

OUTPUT1="/Users/check/1.html"
OUTPUT2="/Users/check/2.html"


When I use one of the above, for instance, I write
c = open(OUTPUT1)
.

However, I think the correct approach for this would be to have something like a generic:

OUTPUT="/Users/check/$var.html"
and then while using, I should first assign:

$var = 1.html
then open(OUTPUT)


This way I open
1.html
.

How do you do this in Python?

Answer

If you wanted to put it in a function:

def getPath(var):
  return "/Users/check/" + str(var) + ".html"

print(getPath(1))
print(getPath(2))
Comments