Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python自动化某些任务?_Python_Astronomy - Fatal编程技术网

如何使用Python自动化某些任务?

如何使用Python自动化某些任务?,python,astronomy,Python,Astronomy,我正在尽可能多地做我的科学展项目,但截止日期快到了。我基本上只是在一个有数千张天文图像的程序中执行这个非常重复的任务 有没有一种通用的方法可以用Python自动化程序任务?我在过去创建过类似的东西(代码如下)。这个程序基本上只是告诉一个程序(SAOIMAGEDS9)打开一个特定文件夹中的文件,并将它们保存为一个新文件夹中的特定文件类型 import os import subprocess import tkinter as tk root = tk.Tk() canvas1 = tk.Can

我正在尽可能多地做我的科学展项目,但截止日期快到了。我基本上只是在一个有数千张天文图像的程序中执行这个非常重复的任务

有没有一种通用的方法可以用Python自动化程序任务?我在过去创建过类似的东西(代码如下)。这个程序基本上只是告诉一个程序(SAOIMAGEDS9)打开一个特定文件夹中的文件,并将它们保存为一个新文件夹中的特定文件类型

import os
import subprocess
import tkinter as tk
root = tk.Tk()

canvas1 = tk.Canvas(root, width = 700, height = 700,)
canvas1.pack()

target_directory = 'C:/dir/' # change this as required
target_write_directory = 'C:/dir/'

def begin_conversion(): #function first conversion
    zipped_files = [x for x in os.listdir(target_directory)if x.lower().endswith('.fits.fz')]
    for filename in zipped_files:
        print(os.path.abspath(filename))
        subprocess.call([r'C:/SAOImageDS9/ds9.exe', os.path.abspath(filename), '-savefits', os.path.abspath(target_write_directory + filename[:-3]), '-exit'])

createBatButton = tk.Button (root, text = '          convert              ', command = begin_conversion)
canvas1.create_window(350,320, window = convert)

root.mainloop()
因此,我在上面的程序中使用tkinter作为GUI,这样我就可以让它保持打开状态,并在需要时按下按钮。这次我不想使用tkinter,那么我应该如何在windows机器上完成这个新任务呢

以下是我到目前为止的情况

import os
import subprocess

target_directory = 'C:/read_directory/' # change this as required make sure to leave a / at the end.
target_write_directory = 'C:/write_directory/' # also change this as needed and leave a / at the end.

def automateProcess(): #Execute the astrometrica commands.
    targetFiles = [x for x in os.listdir(target_directory)if x.lower().endswith('.fits')]
    for filename in targetFiles:
        subprocess.call([r'C:/Astrometrica.exe', os.path.abspath(filename), '-load','-', '-savefits', os.path.abspath(target_write_directory + filename[:-3]), '-exit'])

automateProcess()

root.mainloop()

除了-savefits和-exit,我还需要在程序“Astrometrica”中执行大量非常具体和繁琐的任务。

你的意思是,一旦程序Astrometrica为每个文件打开,就会执行类似于“宏”(重复的击键序列?)的任务?没有通用的方法。一些程序可能有在控制台中运行的选项,而另一些程序可能没有,并且您不能强制他们获取选项。在某些情况下,您可能会尝试使用
PyAutoGUI
pynput
或类似工具来控制鼠标和键盘,并将按键/鼠标事件发送到系统,如果是GUI程序,则可能会将其发送到活动程序。这太模糊/宽泛,可能离题。请参阅:。您应该更具体地说明要自动化的内容和方式:是否希望每特定时间(例如每小时一次)执行某个函数?或者每次它以异步方式接收外部输入时?每次输入是手动(按下按钮)还是完全自动?你应该在循环中执行一次还是多次函数?@jcf我建议你查看我创建的另一篇文章。这篇文章故意显得有些模糊,因为我在谈到具体细节时没有得到任何帮助。()