TypeError: expected str, bytes or os.PathLike object, not int; in subprocess.run()

Question:

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
Asked By: Aidan Keaveney

||

Answers:

The errors says “no int” but you have integers. Just change those to strings.

subprocess.run(["mkclass", txtpath, "libr18", typepath, logpath, "1", "3"])
Answered By: tdelaney

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.

Example of code and error message

Answered By: Via_fx_24
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.