【问题标题】:python can't find file on systempython在系统上找不到文件
【发布时间】:2020-11-14 11:27:01
【问题描述】:

我正在尝试在 linux 服务器上运行 python 脚本,它需要访问文本文件中的列表 - 保存在与我运行脚本相同的位置。

这可能只是一个简单的格式问题,但我尝试了各种方法,仍然没有运气。

这是我的python文件中的代码:

list1 = session.target_list("C:\\root\\instapy-quickstart\\apples.txt")
session.follow_by_list(list1)

得到错误:

No such file or directory: C:\root\instapy-quickstart\apples.txt

【问题讨论】:

  • 路径错误,在 linux 服务器上它看起来像 /home/your_user/file.txt。或者,您可以指定相对于您运行脚本的位置的文件位置,例如 ./apples.txt
  • 在使用反斜杠时应始终使用原始字符串:session.target_list(r"C:\root\instapy-quickstart\apples.txt") - 请注意引号前的r。或者只使用正斜杠,即使在 Windows 上也是如此。

标签: python directory location


【解决方案1】:

如果文件在同一个位置,为什么不使用没有路径的文件名?我的意思是:

list1 = session.target_list("apples.txt")

【讨论】:

  • @mattygee 很高兴听到这个消息:)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多