python - 通过 SSH 运行 Python 脚本时出错

标签 python linux ssh

我正在尝试通过本地 ssh 为 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'

最佳答案

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

关于python - 通过 SSH 运行 Python 脚本时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53344592/

相关文章:

python - 如何使用 Pandas 数据框获取 tfidf?

python - 如何使用 python 获取 Excel 工作表的 "Total Editing Time"属性?

linux - 如何在不修改字符串内容的情况下回显包含引号的字符串?

linux - 如何通过串口采集语音PCM数据并进行处理?

java - 通过 JSCH (SSH) 和 HTTPS 的反向隧道

python - python自动关闭SSH和Telnet吗

python - 如何找到能被整数10整除的多数整数?

python - 在 groupby 循环中编号和创建数据帧

linux - 创建 CentOS 7 KVM guest 时出现问题

authentication - 在不使用公钥身份验证或期望的情况下自动化 SSH(1)