python - 为什么从 IDLE 调用时 Numpy.loadtxt 工作,从 shell 调用时给出 IOError?
问题描述
我尝试执行以下操作:
1 VBA脚本调用shell使用
RetVal = Shell(fullpythonexepath & fullscriptpath)
2 Shell 获取以下命令
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')
当我运行它时,输出文件不会被打印出来,这意味着脚本不能正常运行。
现在来个有趣的事情:当我从 IDLE 运行 python 脚本时,它运行良好。当我使用以下命令从 shell 运行它时:
fullpythonexepath & fullscriptpath
它说 :
我尝试将完整路径输入到 input.txt 和 output.txt。当我这样做并在 IDLE 中运行它时,它不再找到该文件。当从 shell 调用时,它也不起作用。
Python 显然没有找到 input.txt 的路径
据我了解,这个问题与 VBA 无关。使用 shell 命令时也会出现该错误
解决方案是:
import os
os.chdir(r"fullpath")
在我的脚本中,这会将当前工作目录更改为我的路径,然后找到 input.txt。
解决方案
当您从 shell 启动脚本时,脚本的工作目录将是调用它的目录。大多数 IDLE 都定义了自己的工作目录。要检查我建议这样做:
os.getcwd()
在这两种情况下,看看在这两种情况下都使用了什么目录
推荐阅读
- java - 如何在 Spring Security 中为两个不同的 URL 指定两个不同的登录表单?
- elasticsearch - 我如何从我的 groovy 脚本中发出日志
- r - 将持续时间格式化为 ISO 8601
- angular - 角度测试:返回可观察对象的方法已成功测试,但测试覆盖率仍将其标记为未覆盖语句
- sorting - Google Sheet 过滤器功能不能用作排序器
- android - 在 android 中实现 Woocommerce POST 请求
- java - 如何将时间添加到当前时间?
- database - mongo 3.4到3.6升级后Mongo E11000重复密钥错误收集
- swiftui - SwiftUI:取消父视图上的 TapGesture
- keras - keras 评估数据生成器