0

I am hoping to get some insights on how to work with a function on Python. I had a working version of the script that is not great so I tried defining this function that I will use on a different file:

def controlfiles():
    domain = f'domain'
    if os.path.exists(domain):
        with open(domain, 'r') as df:
            domainoutput = df.readline()
    else:
            print("Could not load domain control file")
            exit()

etc, etc. I open a couple more files and at the end I use:

values = [domainoutput, serveroutput, fflavoroutput, toflavoroutput, whooutput, date]
worksheet.append_row(values)

But I get:

values = [domainoutput, serveroutput, fflavoroutput, toflavoroutput, whooutput, date]
NameError: name 'domainoutput' is not defined

I am a total beginner at Python but I read that with open(file) was better than file=open()

Besides that I am trying to get the server time + 7 days in RF3339 to set up a Google Calendar Event, so far I have:

rtime = datetime.datetime.utcnow()
rtformatted = rtime.isoformat("T") + "Z"

Which works for the current time but not for 7 days in the future. I tried deltatime but could not get the right format. Any help is appreciated.

  • Seems like you should `return` the line from your function and capture it when you call the function. – khelwood Nov 21 '20 at 18:07
  • Read the dupe - your variable is out of scope. You can `return domainoutput` after reading the single line from the file (why only 1 line? thats all in it?). You call the function and assing the return of it to some other variable: `other = controlfiles()` on the outside to have something to work with. – Patrick Artner Nov 21 '20 at 18:07
  • Maybe this helps you as well: https://stackoverflow.com/questions/32409802/basic-explanation-of-python-functions – Patrick Artner Nov 21 '20 at 18:08

0 Answers0