在 Python 中每 30 分钟读取一个文件
Reading a file every 30 minutes in Python
正如您在下面的代码中看到的,可以打开目录中的文件并读取它。现在我希望 live_token 每 30 分钟读取一次文件并打印出来。任何人都可以在这方面帮助我吗?
我发现下面的代码是安排工作的时间,但我不知道如何进行必要的修改。
schedule.every(30).minutes.do()
抱歉,如果这个问题太基础了,我对 Python 很陌生。
def read_key():
live_key_file_loc = r'C:\key.txt'
live_key_file = open(live_key_file_loc , 'r')
global key_token
time.sleep(6)
live_token=live_key_file.read()
print(live_token)
@jonrsharpe 是对的。参考schedule usage。你应该有一个脚本,它应该保持 运行ning 总是每 30 分钟从文件中获取令牌。我在下面放了一个应该适合你的脚本。如果您不想 运行 这个文件总是 python,请寻找实施计划作业。
import schedule
import time
def read_key():
with open('C:\key.txt' , 'r') as live_key_file_loc
live_token = live_key_file_loc.read()
print(live_token)
schedule.every(30).minutes.do(read_key)
while True:
schedule.run_pending()
time.sleep(1)
这个过程有几个步骤。
搜索“Task Scheduler”并打开Windows Task Scheduler GUI。
转到操作 > 创建任务...
为您的操作命名。
在“操作”选项卡下,单击“新建”
通过在命令行中输入 where python
找到您的 Python 路径。复制结果并将其放入 Program/Script 输入。
在“添加参数(可选)”框中,输入脚本的名称。前任。 - 在“C:\user\your_python_project_path\yourFile.py”中,输入“yourFile.py”.
在“开始于(可选)”框中,输入脚本的路径。前任。 - 在“C:\user\your_python_project_path\yourFile.py”中,输入“C:\user\your_python_project_path”.
点击“确定”。
转到“触发器”>“新建”并选择您想要的重复。
有关详细信息,请查看此站点 -
https://www.jcchouinard.com/python-automation-using-task-scheduler/
import time
sleep_time = 30 * 60 # Converting 30 minutes to seconds
def read_key():
live_key_file_loc = r'C:\key.txt'
live_key_file = open(live_key_file_loc, 'r')
global key_token
time.sleep(6)
live_token = live_key_file.read()
print(live_token)
while(True): # This loop runs forever! Feel free to add some conditions if you want!
# If you want to read first then wait for 30 minutes then use this-
read_key()
time.sleep(sleep_time)
# If you want to wait first then read use this-
time.sleep(sleep_time)
read_key()
正如您在下面的代码中看到的,可以打开目录中的文件并读取它。现在我希望 live_token 每 30 分钟读取一次文件并打印出来。任何人都可以在这方面帮助我吗? 我发现下面的代码是安排工作的时间,但我不知道如何进行必要的修改。
schedule.every(30).minutes.do()
抱歉,如果这个问题太基础了,我对 Python 很陌生。
def read_key():
live_key_file_loc = r'C:\key.txt'
live_key_file = open(live_key_file_loc , 'r')
global key_token
time.sleep(6)
live_token=live_key_file.read()
print(live_token)
@jonrsharpe 是对的。参考schedule usage。你应该有一个脚本,它应该保持 运行ning 总是每 30 分钟从文件中获取令牌。我在下面放了一个应该适合你的脚本。如果您不想 运行 这个文件总是 python,请寻找实施计划作业。
import schedule
import time
def read_key():
with open('C:\key.txt' , 'r') as live_key_file_loc
live_token = live_key_file_loc.read()
print(live_token)
schedule.every(30).minutes.do(read_key)
while True:
schedule.run_pending()
time.sleep(1)
这个过程有几个步骤。
搜索“Task Scheduler”并打开Windows Task Scheduler GUI。
转到操作 > 创建任务...
为您的操作命名。
在“操作”选项卡下,单击“新建”
通过在命令行中输入
where python
找到您的 Python 路径。复制结果并将其放入 Program/Script 输入。在“添加参数(可选)”框中,输入脚本的名称。前任。 - 在“C:\user\your_python_project_path\yourFile.py”中,输入“yourFile.py”.
在“开始于(可选)”框中,输入脚本的路径。前任。 - 在“C:\user\your_python_project_path\yourFile.py”中,输入“C:\user\your_python_project_path”.
点击“确定”。
转到“触发器”>“新建”并选择您想要的重复。 有关详细信息,请查看此站点 -
https://www.jcchouinard.com/python-automation-using-task-scheduler/
import time
sleep_time = 30 * 60 # Converting 30 minutes to seconds
def read_key():
live_key_file_loc = r'C:\key.txt'
live_key_file = open(live_key_file_loc, 'r')
global key_token
time.sleep(6)
live_token = live_key_file.read()
print(live_token)
while(True): # This loop runs forever! Feel free to add some conditions if you want!
# If you want to read first then wait for 30 minutes then use this-
read_key()
time.sleep(sleep_time)
# If you want to wait first then read use this-
time.sleep(sleep_time)
read_key()