Reputation: 145
In our company, we have a shared folder to which I read and write (mostly CSV files) using Python. I create Python scripts and my teammates run on their windows systems which have different path separators as follows.
/mnt/sherpa/Marketing
'S:\\Marketing\\
/Volumes/sherpa/Marketing/
How do I make these scripts portable to avoid manually changing the file paths as I send these scripts across to other OS?
I have considered solutions like pathlib, os.path but these don't work well with shared folders. Currently using below code to identify the OS on which the python script is being used and then select the path accordingly.
Is there a better way to handle this?
from sys import platform
if platform == "linux" or platform == "linux2":
# linux
elif platform == "darwin":
# OS X
elif platform == "win32":
# Windows...
Upvotes: 2
Views: 1405
Reputation: 32063
Your solution seems succinct and readable, so if it works, why make it harder/more complicated? The paths are completely different, after all.
Note you can use / as a path separator in Python for Windows. Make sure to use os.path.join if you are adding any further elements.
The only thing you might want to do for all platforms would be to load the path from a data/ini file rather than hard-coding.
– barny
Upvotes: 1