Skip to content

Instantly share code, notes, and snippets.

@jtr109
Last active December 14, 2018 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtr109/4ce02fc41c22335ae2d80260ffe40957 to your computer and use it in GitHub Desktop.
Save jtr109/4ce02fc41c22335ae2d80260ffe40957 to your computer and use it in GitHub Desktop.
Find current directory and file's directory

Find current directory and file's directory

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the file constant is relative to the current working directory and is not changed by an os.chdir() call.)


To get the current working directory use

import os
cwd = os.getcwd()

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment