Skip to main content

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_MacroValuest'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

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 


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.


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