Tales From the Data

~an informal portfolio~

Windows File Paths

Another day, another Windows quirk. Today I found an explanation and trick (read, proper way) to write a file path in Python (or probably any language). I am sure this has been the reason behind inconsistent "relative path" success many times in the past year or so of working with data.

The most recent head-against-wall experience:

This ...

In [ ]:
import pandas as pd
train = pd.read_csv ('data\labeledTrainData.tsv')

...works fine.

This however ...

In [ ]:
train = pd.read_csv ('data\testData.tsv')

DOES NOT. head-against-wall-gif

It returns a lengthy file not found error. This time I googled the problem I was having, and actually found a solution. The fix is to simply use double backslashes \\ in all paths.

In [ ]:
train = pd.read_csv ('data\\testData.tsv')

Voila! Works great.

But why?! Oh, because the file path is a nothing more than a string, and backslashes can begin escape characters, such as \n for a new line, or \t for tab- since \l and many other letters have no special meaning, they pass through fine.

This seems obvious, but I sometimes instinctively expect parameters to be like "forms" and interpret with more relevant restrictions .

Why am I even using backslash? I suppose I get in the habit from using windows CLI, and I swear I've worked with something that required it, but I can't recall what.

Mystery solved! (TY Google.)

Comments