votes up 2

No such file: '(fname)'

Package:
numpy
github stars 18118
Exception Class:
IOError

Raise code

    # A file handle
    if hasattr(fname, 'readline'):
        return fname
    # Try to open the file and guess its type
    try:
        f = open(fname)
    except IOError as e:
        raise IOError(f"No such file: '{fname}'") from e
    if f.readline()[:2] != "\\x":
        f.seek(0, 0)
        return f
    f.close()
    raise NotImplementedError("Wow, binary file")

😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

Ways to fix

votes up 0 votes down

Summary: make sure the text file exists or you used the correct path to the file;

Code to reproduce the error:

from numpy.ma.mrecords import fromtextfile
import tempfile
import os
random_content = ("""
    'One (S)','Two (I)','Three (F)','Four (M)','Five (-)','Six (C)'
'strings',1,1.0,'mixed column',,1
""")
with tempfile.TemporaryDirectory() as tmpdir:
    temp_file = os.path.join(tmpdir,"file.txt")
    with open(temp_file, 'w') as f:
        f.write(random_content)
    my_text_file = "wrong_file_path.txt" # none existng file path(incorrect file path)
    mrectxt = fromtextfile(my_text_file, delimitor=',')
    print("content from file\n\n",mrectxt)

Fixed version of the code:

from numpy.ma.mrecords import fromtextfile
import tempfile
import os
random_content = ("""
    'One (S)','Two (I)','Three (F)','Four (M)','Five (-)','Six (C)'
'strings',1,1.0,'mixed column',,1
""")
with tempfile.TemporaryDirectory() as tmpdir:
    temp_file = os.path.join(tmpdir,"file.txt")
    with open(temp_file, 'w') as f:
        f.write(random_content)
    my_text_file = temp_file #Here we are using the correct file path created above
    mrectxt = fromtextfile(my_text_file, delimitor=',')
    print("content from file\n\n",mrectxt)

May 17, 2021 kellemnegasi answer
kellemnegasi 31.6k

Add a possible fix

Please authorize to post fix