Search code examples
pythonpandasoperating-systemglob

Concatenating string and filepath adds extra backslash in pandas


This is almost embarrassingly simple, but I couldn't find a way around this at all. My code used to work just fine as well, and nothing fundamental has changed. Honestly, I can't see the mistake.

In the following code whenever I run a pd.read_excel, I get an extra backslash in my concatenation:

sports_data = [pd.read_excel(r'Data\NData' + str(season) + ".xlsx") for season in season_list]

All I want is the filepath beginning, to be tied to the season variable I set earlier in a list.

Thanks, and trust me that I checked high and wide for this!

I've tested the same with double slashes, as well as a raw string. None of them work as each time the backslashes are doubled. I've even separated the filepath, but it still doesn't work

The error I get is the following, each time:

FileNotFoundError: [Errno 2] No such file or directory: 'Data\\NData2002-2003.xlsx'

If I attempt to run a double-backslash to get the line skip to be a simple backslash, it is still doubled:

FileNotFoundError: [Errno 2] No such file or directory: 'Data\\\\NData2002-2003.xlsx'

Solution

  • This behavior is consistent with Python's normal behavior. Try it out in the REPL:

    >>> s = r'path\to\my\file'
    >>> s
    'path\\to\\my\\file'
    >>> print(s)
    path\to\my\file
    

    A raw string literal still gets stored as a normal string internally, and backslashes still need to be escaped in Python when getting a filepath. Are you sure that the file exists? Double-check the filename and type to see that they match exactly.