MCG Code
MCG Code

Reputation: 1393

Pandas: How to write the path (of where data is) in Linux and windows

I have data stored in a folder in a drive that Windows 7 knows as Drive E. I have dual boot, windows and Linux (ubuntu) My data in Linux is in a folder under /media/username/AC3267F63267C3C4/foldername

In Windows I would write:

from os import path
df = pandas.read_csv(os.path.join("e:/", "Datasets", "datafolder", "datafile.csv"))

But in Linux, for the same source data, what should I use? I can't use drive E right? And if I need to /media/username/AC3267F63267C3C4/foldername what is the point of having os.path? because it was supposed to be cross platform. So how do I access the same data source in Linux?

Upvotes: 0

Views: 2081

Answers (1)

Rakesh
Rakesh

Reputation: 82765

Possible solution. You can use the platform module to detect the OS.

import platform
if platform.system() == 'Windows':
    df = pandas.read_csv(os.path.join("e:/", "Datasets", "datafolder", "datafile.csv"))
else:
    df = pandas.read_csv("/media/username/AC3267F63267C3C4/foldername/datafile.csv")

Upvotes: 1

Related Questions