I’m having a syntax error with an if statement. It was working correctly with a different version, but I’m writing to an output log and I didn’t like how it would output for every file it checked, I want it to only write once if the file exists or not.
The first code below is the one that is not working, it says the the third file is an undefined variable (fifth line of code).
The second code block is how it was working before.
Anyone know how to structure this?
if any(file.endswith('.ppt') for file in os.listdir(scanDestinationPath)):
os.startfile(machineFolderDir + machineType + '\\' +
partNumber + ' REV ' + revisionNumber + '\\' +
file, 'print')
errorLog = open(logBookDir + 'log.txt', 'a+')
errorLog.write('\nA setup sheet called PROG' + programNumber +
' ' + partNumber + ' ' + revisionNumber +
'.ppt was printed.\n')
errorLog.close()
else:
errorLog = open(logBookDir + 'log.txt', 'a+')
m = ('The exception occurred in printDecoSetupSheet().There does not appear '
f'to be a .ppt setup sheet file in folder {partNumber} {revisionNumber} '
f'under {machineType}. Moving on...\n')
errorLog.write(m)
errorLog.close()
Second code block:
if file.endswith(".ppt"):
os.startfile(machineFolderDir + machineType + '\\' +
partNumber + ' REV ' + revisionNumber + '\\' +
file, 'print')
errorLog = open(logBookDir + 'log.txt', 'a+')
errorLog.write('\nA setup sheet called PROG' + programNumber +
' ' + partNumber + ' ' + revisionNumber +
'.ppt was printed.\n')
errorLog.close()
else:
errorLog = open(logBookDir + 'log.txt', 'a+')
m = ('The exception occurred in printDecoSetupSheet().There does not appear '
f'to be a .ppt setup sheet file in folder {partNumber} {revisionNumber} '
f'under {machineType}. Moving on...\n')
errorLog.write(m)
errorLog.close()
The traceback is:
Exception has occurred: NameError name 'file' is not defined File "C:\Users\MacalusoC\Desktop\Technical Docs\TLC_Program_Release\Scripts\Program_Release_v4.py", line 348, in printDecoSetupSheet file, 'print') File "C:\Users\MacalusoC\Desktop\Technical Docs\TLC_Program_Release\Scripts\Program_Release_v4.py", line 835, in main printDecoSetupSheet(scanDestinationPath) File "C:\Users\MacalusoC\Desktop\Technical Docs\TLC_Program_Release\Scripts\Program_Release_v4.py", line 869, in <module> main()