为什么会这样Numpy.loadtxt文件从空闲调用时工作,从shell调用时给出IOError?

2024-10-03 15:33:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着做到以下几点:

1 VBA脚本使用

RetVal = Shell(fullpythonexepath & fullscriptpath)

2 Shell get following命令

fullpythonexepath & fullscriptpath

3 Python脚本

import numpy as np

thefilepath = "input.txt"    # is in the same folder as the script
theoutputpath = "output.txt" # is in the same folder as the script

ssc=np.loadtxt(thefilepath, delimiter='\t', skiprows=0, usecols=range(2)) #error line

#some other code to fit input data

np.savetxt(theoutputpath, sscnew, header=top, fmt='%1.2f\t%1.2f') 

当我运行这个时,输出文件没有被打印出来,这意味着脚本不能正常运行。你知道吗

有趣的是:当我从空闲状态运行python脚本时,它运行得很好。当我使用命令从shell运行它时:

fullpythonexepath & fullscriptpath

上面写着:

enter image description here

  • 我试着把完整的路径输入到输入.txt以及输出.txt. 当我这样做,并在闲置运行它不会找到文件了。从shell调用时,它也不起作用。

  • Python显然找不到输入.txt

  • 据我所知,这个问题与VBA无关。使用shell命令时也会发生此错误

解决办法是:

import os 
os.chdir(r"fullpath")

在我的脚本中,这会将当前工作目录更改为我的路径,然后输入.txt找到了。你知道吗


Tags: theimport命令txt脚本inputisas
1条回答
网友
1楼 · 发布于 2024-10-03 15:33:11

从shell启动脚本时,脚本的工作目录将是调用它的目录。 大多数空闲用户定义自己的工作目录。 要检查,我建议执行以下操作:

os.getcwd()

在这两种情况下,查看两种情况下使用的目录

相关问题 更多 >