首页 > 解决方案 > 为什么 numpy 显示 IOError("%s was not found." %path)?

问题描述

我正在编写一个 pygame 程序,我从一个文件夹中导入所有 .csv 文件。我能够导入所有文件,因为我打印了文件名并显示了我的所有.csv文件。我的问题是它适用于 file_test1.csv,file_test2.csv但它不适用于其他文件(file_test3.csvfile_test4.csv)。它给出了IOError("%s not found." % path)错误_datasource.py file of numpy library。这是我的代码片段:

import pygame
import sys
import os
import csv
import numpy

# Folder Path
path = "D:/Python/Pythonprograms/data"

# Reading out the csv files
def read_text_file(file_path):
    with open(os.path.join(path, file_path)) as f:
        reader = csv.reader(f)
        next(reader, None) # Skip the header
        row_count, col_count = numpy.genfromtxt(file_path, delimiter = ',').shape # The error is shown at this point which leads to numpy's _datasource.py file.
        

# To check if the file exists in the current path
def check_file_names(file_name):
     for file in os.listdir(path):
        print(file)    # I have 6 files in the folder and it correctly displays all the 6 files
        if file_name in file and file.endswith(".csv"): 
            read_text_file(file)    

我调用这个函数,文件名(例如:test1test2)是文件夹中文件的前缀名

def test1():
    check_file_names('test1')
def test2():
    check_file_names('test2')
def test3():
    check_file_names('test3')

有没有人遇到过这个问题?请提供任何建议,说明为什么会发生这种情况。

标签: pythonnumpycsvioerror

解决方案


推荐阅读