0

My python program need to open (or create if they don't exist) a lot of files, so when I want to open e. g "output.txt", I just need to openfile(output.txt) to have it open. However I need to access the data once the file is open, and I would like to give it the name of the file. How do I name my var after the "name" argument here ?

def openfile(file, name):
  global name
  if os.path.isfile('./{0}'.format(file)):
    name = open(file,"w")
  else:
    os.mknod(file)
    name = open(file, "w")
  • Why would you want to pollute your global namespace by creating additional variables? Create a `dict` and store the filenames as keys and the file handles (or their content) as values. – zwer Apr 13 '18 at 11:24
  • You're totally right ! Thanks a lot everyone :) – Alternis Dim Apr 13 '18 at 11:28

0 Answers0