【问题标题】:How to load files that are specified in a .txt file?(python)如何加载 .txt 文件中指定的文件?(python)
【发布时间】:2021-06-17 02:28:59
【问题描述】:

我尝试使用此代码,但它一直说找不到文件并且它们都在同一个文件夹中。感谢您的帮助!

import numpy as np

with open('datasort.txt') as t:
    lines = t.readlines()
    
for i in lines:
    x= np.loadtxt(i)

【问题讨论】:

  • 区分大小写?
  • 由于 t.readlines() 中的每一行都有一个尾随 '\n',请尝试:np.loadtxt(i.rstrip()) 删除每个读取的文件名中的 '\n'。
  • 这不是numpy 问题。您需要以某种方式或其他方式获取正确的目录和/或文件名。

标签: python numpy file


【解决方案1】:

尝试指定文件的完整路径。例如:

with open('\BigFolder\LittleFolder\datasort.txt') as t: lines = t.readlines()

【讨论】:

    猜你喜欢
    • 2021-07-03
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多