2

I converted my python project to a .command file on mac. I was using relative paths to import various CSV files as follow:

import pandas as
df = pd.read_csv('my_file.csv')

It works perfectly since the code and the csv are in the same directory.

Now I converted the python project into a .command executable file by using the following method:

Added first line of Python script as #!/usr/bin/env python

Changed the extension of the file to .command

In Terminal used command chmod +x Test.command

The relative paths don't work anymore and I have to enter the entire path. How can make the paths relative again?

  • Seems for me this is an Mac OS releated problem on linux this will work files relativ to current working directory – Lee Jul 03 '19 at 11:32

1 Answers1

2

use OS library

import os
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'path/where/to/go')

here it was answered for you: Relative paths in Python

StyleZ
  • 1,276
  • 3
  • 11
  • 27