【问题标题】:Use path as inputile instead of file name?使用路径作为输入而不是文件名?
【发布时间】:2020-07-20 17:24:23
【问题描述】:

我正在尝试使用路径而不是文件名作为输入。

我试过了

from pathlib import path
file_to open = Path (source/input*/in.txt)
with open ('file_to_open', 'r') as f_i:
   for line in f_i:

我无法打开路径。请建议我使用路径而不是文件名打开文件的有效方法。

【问题讨论】:

  • 您是否要求用户输入他们选择的文件路径?
  • @AlexDouglas 我想使用路径而不是使用“in.txt”作为输入。不需要是用户的选择。
  • 所以您只想查找input* 路径中的所有文件? in.txt 是您想要的文字吗?我不清楚您的要求。
  • @Alex Douglas 我想搜索所有以输入开头的文件并找到 in.txt 文件

标签: python python-3.x file-handling filehandle


【解决方案1】:

你可以试试这个:

...
file_to_open = Path("source/input*/{}.txt".format(input))
...

基于输入input

【讨论】:

  • 表示输入未定义
  • input = input('File name') 放在file_to_open 行之前
  • 不确定哪里出了问题。它仍然给我错误
猜你喜欢
  • 1970-01-01
  • 2020-04-04
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 2017-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多