I'm writing a program to analyze hundreds of thousands of astronomical data files and classify the objects. I've gotten to the very last step - the files are all ready to be passed to the classification software, but the software requires using the command line. So I'm using subprocess.run() to access the command line from my Python script. When I run this code as is, I get the following error:
Traceback (most recent call last):
File "iterator.py", line 30, in <module>
subprocess.run(["mkclass", txtpath, "libr18", typepath, logpath, 1, 3]) #Passes the txt file to the MKCLASS script
File "/opt/anaconda3/lib/python3.7/subprocess.py", line 472, in run
with Popen(*popenargs, **kwargs) as process:
File "/opt/anaconda3/lib/python3.7/subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "/opt/anaconda3/lib/python3.7/subprocess.py", line 1453, in _execute_child
restore_signals, start_new_session, preexec_fn)
TypeError: expected str, bytes or os.PathLike object, not int
Here's the relevant code. Everything up until the last line of code works as expected (at least best I can tell!)
import os
from astropy.io import fits
import sys
import subprocess
from spelunker import *
fitsdir = sys.argv[1] #Directory to find fits files
txtdir = sys.argv[2] #Directory to find/put txt files
typedir = sys.argv[3] #Directory to put output files (output is star type)
logdir = sys.argv[4] #Directory to put log files (process MKCLASS took)
for spec in os.listdir(fitsdir): #Iterates through each spectrum file in spectrum directory
specpath = os.path.join(fitsdir,spec) #Defines the full path to the spectrum
txtpath = os.path.join(txtdir, spec) #Defines the full path for the txt file to be
hdul = fits.open(specpath, ignore_missing_end=True) #Accesses spectrum file
if hdul[2].data.field('CLASS')[0] != "STAR": #Checks if file is a star
#print("File " +str(specpath) +" is not a star") #use for testing only
continue
filemaker(fluxGetter(hdul),waveGetter(hdul), txtpath.strip(".fits")) #Converts spectrum to txt file and puts it in proper directory
#print("done") #use for testing only
hdul.close() #Closes spectrum file
if len(os.listdir(fitsdir)) != len(os.listdir(txtdir)):
print("Some files were not stars")
for txt in os.listdir(txtdir): #iterates through each txt file in directory
txtpath = os.path.join(txtdir, txt) #Defines the full path to the txt file
typepath = os.path.join(typedir, txt.replace("spec","TYPE")) #Defines the full path for the output to be
logpath = os.path.join(logdir, txt.replace("spec","LOG")) #Defines the full path for the log file to be
subprocess.run(["mkclass", txtpath, "libr18", typepath, logpath, 1, 3]) #Passes the txt file to the MKCLASS script
