Python Question

Should I use open alone in python

As title, Should I use open alone as this situation instead of using f=open(file) and f.close, because I don't know how to close file when I just using open.

More explain: I want to use just open instead of with-statement or f=open() and f.close() because it increase my mini app's size a few Megabytes but I don't know why, so that I just want to know if I use open alone and how it affect my app (or computer) if I could use it? (I'm just a newbie)

def resource_path(relative_path):
try:
open(relative_path)
base_path = os.path.abspath(".")
except Exception:
base_path = sys._MEIPASS
return os.path.join(base_path, relative_path)

Answer

Use with-statement:

def resource_path(relative_path):  
  try:
      with open(relative_path) as file:
          base_path = os.path.abspath(".")
  except Exception:
      base_path = sys._MEIPASS
  return os.path.join(base_path, relative_path)