Search code examples
pythonpandascsvpathfilepath

Why does the filepath become invalid when in reality it is completely valid?


import numpy as np
import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
import matplotlib.pyplot as plt
%matplotlib inline

filepath = "G:\learning python\page view time series\trum.csv"
df = pd.read_csv(filepath, index_col = "date", parse_dates=True)

The above code gives the following error:

OSError: [Errno 22] Invalid argument: 'G:\\learning python\\page view time series\trum.csv'

But if I change the name of my csv file to 'laem.csv' and update the path, then the code runs perfectly.

import numpy as np
import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
import matplotlib.pyplot as plt
%matplotlib inline

filepath = "G:\learning python\page view time series\laem.csv"
df = pd.read_csv(filepath, index_col = "date", parse_dates=True)

Why is this happening?


Solution

  • Because \t actually means "put a tab space here", not litteraly \ and t. You can see this by trying

    print("Hello\tWorld") # "Hello   World"
    

    You can avoid this by either escaping the backslash itself with \\

    print("Hello\\tWorld") # "Hello\tWorld"
    

    or using a raw string by prepending an r to the string literal.

    print(r"Hello\tWorld") # "Hello\tWorld"
    

    You can read about string literals in the docs.