Инициализировать или запустить файл Python из другого сценария Python? [закрыто]Python

Программы на Python
Anonymous
Инициализировать или запустить файл Python из другого сценария Python? [закрыто]

Сообщение Anonymous »

Я новичок в Python. У меня есть сценарий «CronJob.py», запуск которого по расписанию zcron осуществляется каждые 2 часа. Но в настоящее время возникли проблемы с автоматическим обновлением.
Я использовал библиотеки расписаний, но все равно не смог добиться результатов. Пожалуйста, предложите.
Я попробовал этот код в скрипте Python test.py, как показано ниже, где я импортирую CronJob

Код: Выделить всё

import schedule
import time
import datetime as dt
import os
import psutil

log_output_file = 'C:\\Python Scripts\\Production\\Python Scripts\\log.txt'
path_scripts = 'C:\\Python Scripts\\Production\\Python Scripts\\'

def log_file(message, log_output_file, file_attribute):
f = open(log_output_file, file_attribute)
f.write(message)
f.close()

now = dt.datetime.now()
print ("Current date and time : ")
print (now.strftime("%d-%m-%Y %H:%M:%S"))
log_file(now.strftime("%d-%m-%Y %H:%M:%S"), log_output_file, 'w')

# delete any excel workbooks that were opened during a reboot
# system account files
dir='C:\\Program Files\\Microsoft Office\\root\\Office16\\XLSTART'
for root, dirs, files in os.walk(dir):
for name in files:

os.remove(os.path.join(root, name))

# remove any instances of excel still running
for proc in psutil.process_iter():
if proc.name() == "EXCEL.EXE":
proc.kill()

def get_time() -> str:
r'''Get current time and date and return as string.'''
return time.strftime('%X (%d/%m/%y)')

def myjob():
print("I’m working...",get_time())
print('CronJob')
log_file('\n', log_output_file, 'a')
log_file('CronJob', log_output_file, 'a')
exec(open(path_scripts + 'CronJob.py').read())
print('----------')

now = dt.datetime.now()
print ("Current date and time : ")
print (now.strftime("%d-%m-%Y %H:%M:%S"))
log_file('\n', log_output_file, 'a')
log_file(now.strftime("%d-%m-%Y %H:%M:%S"), log_output_file, 'a')

schedule.every(4).minutes.do(myjob)
while True:
schedule.run_pending()
time.sleep(1)
Возможно ли, что CronJob.py сам запускает другие коды Python?
Приведенный выше код выполняется с некоторой ошибкой.


Подробнее здесь: https://stackoverflow.com/questions/783 ... hon-script

Вернуться в «Python»