Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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
                Thanks, I had a .split(' ') at the end of my list! I did command += other_list.split(' ') instead.
– TheTechRobo the Nerd
                Jul 8, 2021 at 22:11

Answer from tdelaney directly solves the problem for me.

However, i would like to add something. I was using Thonny IDE (probably the same behaviour with all IDEs) and the error was not specified at the correct line number.

In the following example/printscreen, the error was reported to be at line 133. However, the integers that should have been strings were on line 127 and 128. After adding str(Duration_of_Video) and str(Frame_Per_Second) in place of Duration_of_Video and Frame_Per_Second without the str, it solves the problem.

I lost time in finding the error because the indicated line number of the error was not correct. In the following printscreen, one should note that the code doesn't correspond to the error message since I have added the two str() to provide a printscreen with the solution. Thus, the code is actually working. Removing the two str() would lead to the error message in red in the printscreen.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.