pandas.read_csv FileNotFoundError: File b'\xe2\x80\xaa' despite correct path

前端 未结 13 1220
南笙
南笙 2020-12-15 20:14

I\'m trying to load a .csv file using the pd.read_csv() function when I get an error despite the file path being correct and using raw strings.

相关标签:
13条回答
  • 2020-12-15 20:57

    $10 says your file path is correct with respect to the location of the .py file, but incorrect with respect to the location from which you call python

    For example, let's say script.py is located in ~/script/, and file.csv is located in ~/. Let's say script.py contains

    import pandas
    df = pandas.read_csv('../file.csv') # correct path from ~/script/ where script.py resides
    

    If from ~/ you run python script/script.py, you will get the FileNotFound error. However, if from ~/script/ you run python script.py, it will work.

    0 讨论(0)
  • 2020-12-15 20:58

    On Windows systems you should try with os.path.normcase.

    It normalize the case of a pathname. On Unix and Mac OS X, this returns the path unchanged; on case-insensitive filesystems, it converts the path to lowercase. On Windows, it also converts forward slashes to backward slashes. Raise a TypeError if the type of path is not str or bytes (directly or indirectly through the os.PathLike interface).

    import os
    import pandas as pd
    
    script_dir = os.getcwd()
    file = 'example_file.csv'
    data = pd.read_csv(os.path.normcase(os.path.join(script_dir, file)))
    
    0 讨论(0)
  • 2020-12-15 20:58

    Try using os.path.join to create the filepath:

    import os
    f_path = os.path.join(*['C:', 'Users', 'user', 'Desktop', 'datafile.csv'])
    df = pd.read_csv(f_path)
    
    0 讨论(0)
  • 2020-12-15 21:00

    Experienced the same issue. Path was correct. Changing the file name seems to solve the problem.

    Old file name: Season 2017/2018 Premier League.csv New file name: test.csv

    Possibly the whitespaces or "/"

    0 讨论(0)
  • 2020-12-15 21:03

    For my particular issue, the failure to load the file correctly was due to an "invisible" character that was introduced when I copied the filepath from the security tab of the file properties in windows.

    This character is e2 80 aa, the UTF-8 encoding of U+202A, the left-to-right embedding symbol. It can be easily removed by erasing (hitting backspace or delete) when you've located the character (leftmost char in the string).

    Note: I chose to answer because the answers here do not answer my question and I believe a few folks (as seen in the comments) might meet the same situation as I did. There also seems to be new answers every now and then since I did not mark this question as resolved.

    0 讨论(0)
  • 2020-12-15 21:05

    I know following is a silly mistake but it could be the problem with your file.

    I've renamed the file manually from adfa123 to abc.csv. The extension of the file was hidden, after renaming, Actual File name became abc.csv.csv. I've then removed the extra .csv from the name and everything was fine.

    Hope it could help anyone else.

    0 讨论(0)
提交回复
热议问题