TypeError:预期的str,字节或os.PathLike对象,而不是int;在subprocess.run()中

问题描述 投票:0回答:1

我正在编写一个程序,以分析成千上万的天文数据文件并对物体进行分类。我已经走到最后一步-文件已经准备好传递给分类软件,但是该软件需要使用命令行。因此,我正在使用subprocess.run()从我的Python脚本访问命令行。当我按原样运行此代码时,出现以下错误:

    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

这里是相关代码。直到最后一行代码,一切都按预期工作(至少我可以告诉!)

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
python
1个回答
0
投票

错误显示为“ no int”,但您有整数。只需将其更改为字符串即可。

subprocess.run(["mkclass", txtpath, "libr18", typepath, logpath, "1", "3"])
© www.soinside.com 2019 - 2024. All rights reserved.