span8
span4
使用这里的逻辑帖子这帖子,我想用Pyhton关机脚本重命名我的日志文件。
由于某些原因,操作系统。重命名只适用于python 2.7而不是python 3.x。
我试过单引号、双引号和三引号;使用raw,我似乎找不到一个好的方法来纠正os.重命名。使用FME_macro和源文件的字符串。到目前为止,一切都没有奏效。
# Import module Import fme, datetime, time, os # Get current date and time datetime = datetime.date.today().strftime('%Y/%m/%d') timeNow = datetime.datetime.now().time().strftime('%H.% m .%S') logger = open(FME_LogFileName,'a') logger = open(FME_LogFileName,'a')写(“日期:{}\ n”.format (dateToday))记录器。写(“时间:{}\ n”.format (timeNow))记录器。写(“ProcessID: {} \ n”.format (fme.processID))记录器。写(“状态:{}\ n”.format (fme.status))记录器。写入('工作区持续时间为{}秒\n'.format(fme.elapsedRunTime))记录器。写日志记录器('Total feature read {}\n'.format(fme.totalFeaturesRead))。写(写的总功能{}\ n”.format (fme.totalFeaturesWritten)) src = str (FME_LogFileName) dirpath = FME_MacroValues [' FME_MF_DIR '] #目录路径如果dirpath.endswith(“/”):dirpath = dirpath(: 1)时间戳= time.strftime (Y ' % % m % d % H % m % S ') id = str (fme.processID) logname = dirpath + ' \ \ ' +身份证+“_”+时间+”。#os.rename(src,logname) print('重命名日志文件…' + logname)
当我rem os的结果。重命名行
我的工作空间版本附呈
嗨@salvaleonrp,我担心脚本即使使用Python 2.7解释器也无法运行,因为打开的文件(“logger”对象)在重命名之前还没有关闭。
我认为如果你在写完附加信息后关闭文件,脚本可以成功运行。
logger =打开(FME_LogFileName,'a') logger。写(“日期:{}\ n”.format (dateToday))…logger.close ()
您也可以在这里使用with语句。在with语句中,打开的文件将自动关闭,而不使用close方法。我建议你使用with语句通常来打开/关闭一个文件。
打开(FME_LogFileName,'a')作为logger: logger。写(“日期:{}\ n”.format (dateToday))…
点击这里了解更多with语句:https://docs.python.org/3.7/reference/compound_stmts.html#the-with-statement
谢谢你的提示@takashi和python注释。不管有没有,它在我身上都能起作用,这让我觉得更奇怪logger.close2.7的声明-见下面的截图。我将尝试使用你的建议与声明。
©2020 S亚搏在线afe Software Inc |法律