0

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()
stovfl
  • 14,998
  • 7
  • 24
  • 51
Chris Macaluso
  • 1,372
  • 2
  • 14
  • 33
  • Exact traceback, please. – chepner Feb 01 '19 at 19:25
  • 2
    `file` isn't defined; it's local to the generator expression passed as an argument to `any`. – chepner Feb 01 '19 at 19:26
  • Try putting [] around `file.endswith('.ppt') for file in os.listdir(scanDestinationPath)` to turn it from a generator exp to a list of bool. – KuboMD Feb 01 '19 at 19:45
  • This did not work, same syntax error, but thank you for the suggestion. I think my best bet is just to add a `break` after the code block in the `else` statement, since what I'm concerned with is multiple outputs to the text file, this would solve it. – Chris Macaluso Feb 01 '19 at 20:20
  • @55thSwiss What you seem to want is the variable to leak out. In a generator expression, it doesn't do that. In Py2, [lists do leak variables](https://stackoverflow.com/questions/4198906/python-list-comprehension-rebind-names-even-after-scope-of-comprehension-is-thi), which is what KuboMD was suggesting... but even that is bad practice. Regarldess, your changed conditional also likely won't do what you expect. `any()` returns True/False, not a list, so it will only happen for one file, whatever the first ppt file it finds is... it won't log, nor process, any other files. – TemporalWolf Feb 01 '19 at 20:26
  • If you [edit] this question to include specifically what you want the result to be, that will trigger a reopen vote and you may end up getting an answer. It would also be helpful to see more of the scope: it's not at all clear what you are trying to accomplish here. see [mcve] for more information. – TemporalWolf Feb 01 '19 at 20:29
  • file is a reserved keyword in python. Can you replace `for file in` with `for _file in` and update relevant references. – Sharad Feb 04 '19 at 07:03
  • Also, please include line numbers in your code-snippet aligning with the traceback. – Sharad Feb 04 '19 at 07:05

0 Answers0