Solved

Using the ProcessID and TimeStamp as my log name - failing on rename with py3.x but not 2.7

  • 14 January 2020
  • 3 replies
  • 1 view

Badge +10

Using the logic from this post and this post, I wanted to rename my log file using the Pyhton shutdown script.

For some reason, os.rename works only with python 2.7 and not python 3.x.

I tried single, double, and triple quotes; used raw, and I can't seem to find a good way to correct os.rename. Used FME_macro and just a string for the source file. Nothing worked so far.

# Import modules
import fme, datetime, time, os

# Get current date and time
dateToday = datetime.date.today().strftime('%Y/%m/%d')
timeNow = datetime.datetime.now().time().strftime('%H.%M.%S')

logger = open(FME_LogFileName,'a')
logger.write('Date: {}\n'.format(dateToday))
logger.write('Time: {}\n'.format(timeNow))
logger.write('ProcessID: {}\n'.format(fme.processID))
logger.write('Status: {}\n'.format(fme.status))
logger.write('Workspace duration was {} seconds\n'.format(fme.elapsedRunTime))
logger.write('Total feature read {}\n'.format(fme.totalFeaturesRead))
logger.write('Total features written {}\n'.format(fme.totalFeaturesWritten))

src = str(FME_LogFileName)

dirpath = FME_MacroValues['FME_MF_DIR'] # directory path
if dirpath.endswith('/'):
dirpath = dirpath[:-1]
timestamp = time.strftime('%Y%m%d%H%M%S')
id = str(fme.processID)
logname = dirpath + '\\' + id + '_' + timestamp + '.log'
print('Source: ' + src)
print('Destination: ' + logname)
#os.rename(src,logname)
print ('Renaming logfile... ' + logname)

Result when I rem os.rename line

0684Q00000ArBayQAF.png

 

My version of the workspace attached

icon

Best answer by takashi 15 January 2020, 00:16

View original

3 replies

Userlevel 2
Badge +17

Hi @salvaleonrp, I'm afraid that the script fails to run even if using Python 2.7 interpreter, because the opened file (the  "logger" object) has not been closed before renaming that.

I think the script could be run successfully if you closed the file after writing additional information.

logger = open(FME_LogFileName,'a')
logger.write('Date: {}\n'.format(dateToday))
...
logger.close()

Alternatively you can use the with statement here. In the with statement, the opened file would be closed automatically without using the close method. I would recommend you to use the with statement usually to open/close a file.

with open(FME_LogFileName,'a') as logger:
    logger.write('Date: {}\n'.format(dateToday))
    ...

See here to learn more about the with statement: https://docs.python.org/3.7/reference/compound_stmts.html#the-with-statement 

Badge +10

Thanks for the tip @takashi and the python notes. It gets weirder as it worked for me with or without the logger.close statement on 2.7 - see screenshot below. I'll try your suggestion using the with statement.

Badge +10

Hi @salvaleonrp, I'm afraid that the script fails to run even if using Python 2.7 interpreter, because the opened file (the  "logger" object) has not been closed before renaming that.

I think the script could be run successfully if you closed the file after writing additional information.

logger = open(FME_LogFileName,'a')
logger.write('Date: {}\n'.format(dateToday))
...
logger.close()

Alternatively you can use the with statement here. In the with statement, the opened file would be closed automatically without using the close method. I would recommend you to use the with statement usually to open/close a file.

with open(FME_LogFileName,'a') as logger:
    logger.write('Date: {}\n'.format(dateToday))
    ...

See here to learn more about the with statement: https://docs.python.org/3.7/reference/compound_stmts.html#the-with-statement 

After using with statement my shutdown script works. Thanks again!

0684Q00000ArMsEQAV.png

Reply