如何将日期时间保存到 Python 中的文件以用于脚本自动化
How to save datetime to file in Python for use in script automation
我正在创建一个脚本,该脚本会定期在服务器上抓取已添加的“新”文件。为此,我希望将上次脚本执行的日期时间存储在一个文件中,以便脚本可以处理自该日期时间以来的“所有新文件”。最终目标是通过 Windows 任务计划程序定期 运行 此脚本。
我可以使用下面的代码来完成此操作的基本版本。但是,我希望有一种更简洁、更短或更健壮的方法来实现这一目标。欢迎提出任何建议!
import datetime
fmt = "%Y-%m-%d %H:%M:%S"
last_run = ""
# try loading the datetime of the last run, else print warning
try:
with open("last_run.txt", mode="r") as file:
last_run = datetime.datetime.strptime(file.read(), fmt)
print(last_run)
except:
print("no file available")
# ... run script code using the last_run variable as input ...
# update the script execution time and save it to the file
with open("last_run.txt", mode="w") as file:
file.write(datetime.datetime.now().strftime(fmt))
您的解决方案看起来不错。
我唯一想建议的是在两个单独的函数中取出读取和写入最后一个 运行 时间戳逻辑,并将这两个函数移动到一个单独的模块文件中。这与上述回复中@Tomalak 的建议相同。下面是代码示例。
模块文件:last_run.py
import datetime
fmt = "%Y-%m-%d %H:%M:%S"
def get_last_run_time_stamp():
"""
Get last run time stamp\n
====\n
When this function called\n
AND last_run.txt file is present\n
Then open the file and read the time-stamp stored in it\n
====\n
When this function is called\n
AND last_run.txt file is not present\n
Then print the following message on console: "last_run.txt file is not available"\n
"""
# try loading the datetime of the last run, else print warning
try:
with open("last_run.txt", mode="r") as file:
return datetime.datetime.strptime(file.read(), fmt)
except:
# Return with current time-stamp if last_run.txt file is not present
return datetime.datetime.now().strftime(fmt)
# ... run script code using the last_run variable as input ...
def save_last_run_time_stamp():
"""
Save last run time stamp\n
====\n
When this function called\n
AND last_run.txt file is present\n
Then Open the file, save it with current time stamp and close the file\n
====\n
When this function called\n
AND last_run.txt file is not present\n
Then Create the file, open the file, save it with current time stamp and close the file\n
"""
# update the script execution time and save it to the file
with open("last_run.txt", mode="w") as file:
current_timestamp = datetime.datetime.now().strftime(fmt);
file.write(current_timestamp)
那么,下面是schedular配置的文件和运行:
run_latest_scaped_files.py,
import last_run as lr
last_run = lr.get_last_run_time_stamp()
print(last_run)
# ... run script code using the last_run variable as input ...
lr.save_last_run_time_stamp()
就是这样了!!!
我正在创建一个脚本,该脚本会定期在服务器上抓取已添加的“新”文件。为此,我希望将上次脚本执行的日期时间存储在一个文件中,以便脚本可以处理自该日期时间以来的“所有新文件”。最终目标是通过 Windows 任务计划程序定期 运行 此脚本。
我可以使用下面的代码来完成此操作的基本版本。但是,我希望有一种更简洁、更短或更健壮的方法来实现这一目标。欢迎提出任何建议!
import datetime
fmt = "%Y-%m-%d %H:%M:%S"
last_run = ""
# try loading the datetime of the last run, else print warning
try:
with open("last_run.txt", mode="r") as file:
last_run = datetime.datetime.strptime(file.read(), fmt)
print(last_run)
except:
print("no file available")
# ... run script code using the last_run variable as input ...
# update the script execution time and save it to the file
with open("last_run.txt", mode="w") as file:
file.write(datetime.datetime.now().strftime(fmt))
您的解决方案看起来不错。
我唯一想建议的是在两个单独的函数中取出读取和写入最后一个 运行 时间戳逻辑,并将这两个函数移动到一个单独的模块文件中。这与上述回复中@Tomalak 的建议相同。下面是代码示例。
模块文件:last_run.py
import datetime
fmt = "%Y-%m-%d %H:%M:%S"
def get_last_run_time_stamp():
"""
Get last run time stamp\n
====\n
When this function called\n
AND last_run.txt file is present\n
Then open the file and read the time-stamp stored in it\n
====\n
When this function is called\n
AND last_run.txt file is not present\n
Then print the following message on console: "last_run.txt file is not available"\n
"""
# try loading the datetime of the last run, else print warning
try:
with open("last_run.txt", mode="r") as file:
return datetime.datetime.strptime(file.read(), fmt)
except:
# Return with current time-stamp if last_run.txt file is not present
return datetime.datetime.now().strftime(fmt)
# ... run script code using the last_run variable as input ...
def save_last_run_time_stamp():
"""
Save last run time stamp\n
====\n
When this function called\n
AND last_run.txt file is present\n
Then Open the file, save it with current time stamp and close the file\n
====\n
When this function called\n
AND last_run.txt file is not present\n
Then Create the file, open the file, save it with current time stamp and close the file\n
"""
# update the script execution time and save it to the file
with open("last_run.txt", mode="w") as file:
current_timestamp = datetime.datetime.now().strftime(fmt);
file.write(current_timestamp)
那么,下面是schedular配置的文件和运行:
run_latest_scaped_files.py,
import last_run as lr
last_run = lr.get_last_run_time_stamp()
print(last_run)
# ... run script code using the last_run variable as input ...
lr.save_last_run_time_stamp()
就是这样了!!!