Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python IOError:[Errno 2]没有这样的文件或目录-在linux上,使用绝对路径_Python_Pytest - Fatal编程技术网

Python IOError:[Errno 2]没有这样的文件或目录-在linux上,使用绝对路径

Python IOError:[Errno 2]没有这样的文件或目录-在linux上,使用绝对路径,python,pytest,Python,Pytest,我正在努力寻找解决办法。我使用的绝对路径肯定存在于文件系统中。在同一脚本中使用相同的精确路径时 if host.file(logPath).exists: print("Exists: " + logPath) 我明白了 但在尝试时: with open(logPath, "rt") as log: 我得到: > with open(logPath, "rt") as log: E IOError: [Errno 2] N

我正在努力寻找解决办法。我使用的绝对路径肯定存在于文件系统中。在同一脚本中使用相同的精确路径时

if host.file(logPath).exists:
    print("Exists: " + logPath)
我明白了

但在尝试时:

with open(logPath, "rt") as log:
我得到:

>               with open(logPath, "rt") as log:
E               IOError: [Errno 2] No such file or directory: '/var/opt/jws/jws3.0/v7/testinfra_node1/logs/catalina.out'
以下是全部代码(这是一个用于测试JWT安装的testinfra脚本):

我也一直在努力

print(host.check_output("cat " + logPath))
它可以很好地打印文件内容

关于如何处理这个问题有什么想法吗? 非常感谢

编辑: 下面是我执行脚本的方式:

py.test-v——主机=user@hosttomcat_test_recycle.py--sudo


testinfra
包似乎将测试远程服务器的状态集成到
pytest
中。因此

host.file(logPath).exists

正在检查远程服务器上的存在和内容,而

open(logPath, "rb") 

正在本地机器上运行。文件不在本地计算机上,它在主机上。文件(logPath)。exists应该是主机。文件(logPath)。exists()@Aran-Fey:我想不是。这一个来自testinfra模块:。这是在您建议的修改之后我得到的:>如果host.file(logPath.exists():E TypeError:'bool'对象不是callable@Idlehands:正如我前面提到的,this.exists是一个属性,而不是一个方法。此外,该文件确实存在,因为我能够使用相同的path变量读取同一脚本中的内容。为什么代码将“rt”作为打开模式,但提供的错误消息显示为“
”rb”
?@Arc676:我从错误的测试结果复制了它。尝试了不同的选择(rt,rb)-结果是一样的。我现在将编辑该问题以修正此差异。谢谢听了你的评论后,我感到震惊。非常感谢你!
print(host.check_output("cat " + logPath))
host.file(logPath).exists
host.check_output("cat " + logPath)
open(logPath, "rb")