通过SSH运行Python脚本时出错

时间:2018-11-16 19:59:37

标签: python linux ssh

我试图通过ssh local在Linux机器上远程运行Python脚本,但是当我的脚本读取txt文件时出现错误,该脚本在Python IDE上运行良好且没有错误。

我正在使用ssh root@ip_adress python2 < script.py运行脚本。

我正在读取txt文件的脚本的一部分:

import os
import smtplib

with open("file.txt") as fp:
     conteudo = fp.readlines()

conteudo = [linhas.strip() for linhas in conteudo]

错误:

Traceback (most recent call last):
File "<stdin>", line 4, in <module>
IOError: [Errno 2] No such file or directory: 'file.txt'

1 个答案:

答案 0 :(得分:1)

主要问题是file.txt仅存在于我的计算机上,因此要正常工作,我需要直接连接到服务器以与其共享文件系统,但这是不安全的。因此,我决定使用scp file.txt root@ip_adress:/path/where/the/file/is/going/to将file.txt传输到我的服务器,然后我不得不将“ with open()as”函数中的路径更改为我的文件将要到达的路径。之后,我可以无错误地执行脚本。