Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Platform-independent file paths?

How can I use a file inside my app folder in Python? Platform independent of course... something similar to this:

#!/bin/sh mypath=${0%/*} LIBDIR=$mypath/modules 
like image 780
Hairo Avatar asked May 17 '11 19:05

Hairo


1 Answers

You can use os.path and its functions, which take care of OS-specific paths:

>>> import os >>> os.path.join('app', 'subdir', 'dir', 'filename.foo') 'app/subdir/dir/filename.foo' 

On Windows, it should print out with backslashes.

like image 74
Blender Avatar answered Oct 17 '22 06:10

Blender