I am fairly new to programmer & 90% of my programming is in the data analyst space. Much of my work is reading in csv or excel files using the Pandas package. I program on a few different machines & use Dropbox to sync the csv files & then Github to manage the code side (not the csv files). The csv files need to stay on Dropbox, as other users of the data read them, too. Depending on what computer I'm on (work Mac vs. home Ubuntu), it's easy enough to read in the csv files on different computers using the OS module (something like this
os.chdir(os.path.join(os.getenv('HOME'), 'Dropbox/etc/etc))
This works regardless of Mac or Linux machine as long as Dropbox is in the default installation spot.
I've recently started using a Windows computer. My question - how does someone who uses Linux & Windows machines work with this? I can't seem to find an equivalent way to pull in the csv files with a Windows machine. I have searched around WSL but haven't been able to find a solution.
The goal is to write code that is agnostic to the OS. Any help would be greatly appreciated!
CodePudding user response:
The best way that works across platform is like below
import os
os.path.expanduser('~/Dropbox/etc/etc')
This command will auto expand the '~'
to your home directory.
CodePudding user response:
If you want to use pathlib
you can use Path.home()
to get the user's home directory or you can expand a given path relative to the user's home directory.
>>> from pathlib import Path
>>> Path.home()
PosixPath("/home/username") # linux
WindowsPath("C:\\Users\\Username") # windows
>>> Path("~/Dropbox/etc/etc").expanduser()
PosixPath("/home/username/Dropbox/etc/etc") # linux
WindowsPath("C:\\Users\\Username\\Dropbox\\etc\\etc") # windows