我在天文学领域工作,我用来解压从望远镜得到的图像的过程可能非常乏味。图片的格式是'fits.fz‘,它代表fits.fit-zipped。我想把它们解压成“.fits”。我已经开发了一个简化这个解压过程的程序。我已经通过Python和Tkinter创建了一个带有两个按钮的图形界面。第一个按钮创建一个名为'list.txt‘的文本文件,然后执行一个预先存在的.bat文件,该文件将特定目录中以'fits.fz’结尾的每个文件的名称转储到'list.txt‘中。第一个按钮还用于将文件的特定名称复制到另一个bat文件中的特定位置。另一个.bat文件名为‘list.txt’,应该对‘list.txt’中的每个文件使用以下命令:
C:\ds9\ds9.exe
C:\directory\FITS FILE HERE
-savefits
我希望python程序能够从一行代码中复制特定的部分,并将它们粘贴到“FITS FILE HERE”所在的位置。
下面是按下第一个按钮时执行的功能。
f = open('C:/jah/list.txt')
f1 = open('C:/jah/decompress.bat', 'a')
def begin_wombocombo(): #Is function for first button
open('C:/jah/list.txt', 'w').close() #Clears 'list.txt'
open('C:/jah/decompress.bat', 'w').close() #Clears 'decompress.bat'
subprocess.call([r'C:/jah/newbat.bat']) #Dumps directory into 'list.txt'
doIHaveToCopyTheLine=False #Bool for whether or not the program has to copy line
for line in f.readlines(): #loops through all instances to find fz files and then pastes them into decompress.bat
if 'fits.fz' in line:
doIHaveToCopyTheLine=True
if doIHaveToCopyTheLine:
f1.write(line)
f1.close()
f.close()
这样做的问题是它只复制包含fits.fz文件的文本行。这意味着它会复制该行上的所有其他内容,例如创建文件的时间。有没有办法简单地只复制和粘贴fits.fz文件?我该如何将这些字符串放入.bat文件呢?
感谢您的时间,顺便说一句,第二个按钮只是执行'decompress.bat‘,这是一个带有解压图像命令的文件。
发布于 2019-06-20 07:48:13
我认为在Python中,像这样的东西可以做到这一点,而不需要写出批处理文件等。
import os
import subprocess
target_directory = 'C:\\directory\\' # change this as required
zipped_files = [x for x in os.listdir(target_directory)
if x.lower().endswith('.fits.fz')]
for filename in zipped_files:
subprocess.call([r'C:\ds9\ds9.exe', os.path.abspath(filename), '-savefits'])
https://stackoverflow.com/questions/56677085
复制相似问题