1

How can I set the current path of my python file "myproject.py" to the file itself? I do not want something like this:

path = "the path of myproject.py"

In mathematica I can set:

SetDirectory[NotebookDirectory[]]

The advantage with the code in Mathematica is that if I change the path of my Mathematica file, for example if I give it to someone else or I put it in another folder, I do not need to do anything extra. Each time Mathematica automatically set the directory to the current folder.

I want something similar to this in Python.

MOON
  • 2,516
  • 4
  • 31
  • 49
  • What exactly are you trying to do and why? Does `myproject.py` need to be imported or just ran from the command line? – msvalkon Feb 21 '14 at 12:40
  • `os.chdir('/path')` or `os.getcwd()`? – Torxed Feb 21 '14 at 12:40
  • In the myproject.py I have to import some txt files and plot them. os.getcwd() gives the directory which python uses. I want to set the directory in which the python file is, as working directory. – MOON Feb 21 '14 at 12:47

3 Answers3

5

The right solution is not to change the current working directory, but to get the full path to the directory containing your script or module then use os.path.join to build your files path:

import os
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))

# then:
myfile_path = os.path.join(ROOT_PATH, "myfile.txt")

This is safer than messing with current working directory (hint : what would happen if another module changes the current working directory after you did but before you access your files ?)

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • 1
    @yashar: note: `__file__` fails if the Python file is executed using `execfile('/path/to/myproject.py')`. [`inspect`-based solution](http://stackoverflow.com/a/21935564/4279) continues to work. – jfs Feb 21 '14 at 13:45
  • @J.F.Sebastian: thanks for pointing this - I very seldom use `execfile` so never encountered that problem. – bruno desthuilliers Feb 21 '14 at 17:52
2

I want to set the directory in which the python file is, as working directory

There are two step:

  1. Find out path to the python file
  2. Set its parent directory as the working directory

The 2nd is simple:

import os
os.chdir(module_dir) # set working directory

The 1st might be complex if you want to support a general case (python file that is run as a script directly, python file that is imported in another module, python file that is symlinked, etc). Here's one possible solution:

import inspect
import os

module_path = inspect.getfile(inspect.currentframe())
module_dir = os.path.realpath(os.path.dirname(module_path))
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

Use the os.getcwd() function from the built in os module also there's os.getcwdu() which returns a unicode object of the current working directory

Example usage:

import os
path = os.getcwd()
print path
#C:\Users\KDawG\Desktop\Python
K DawG
  • 13,287
  • 9
  • 35
  • 66
  • 2
    Question asks how to *set* not get. – Janne Karila Feb 21 '14 at 12:50
  • @JanneKarila I'm quite confused by what your saying but this is clearly what the OP needs, read the docs – K DawG Feb 21 '14 at 12:53
  • In the myproject.py I have to import some txt files and plot them. os.getcwd() gives the directory which python uses. I want to set the directory in which the python file is, as working directory. – yashar – MOON Feb 21 '14 at 12:59