gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:33:41 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com