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")

🙏 Scream for help to Ukraine
Today, 2nd July 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

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 30.0k

Add a possible fix

Please authorize to post fix