Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xaviergoby/039e30d10435709915e51c8acf16a5c4 to your computer and use it in GitHub Desktop.
Save xaviergoby/039e30d10435709915e51c8acf16a5c4 to your computer and use it in GitHub Desktop.
Just a collection of useful Python code snippets for working with file system on Windows (e.g. how to retrieve the path to the directory containing a Python script you want to run etc...)
# 1]
# Get the absolute path to directory which contains the Python script you want to/are run(ning)
# Note: Folder <==> Directory
# E.g.: Absolute (abs) path to the directory (dir): C:\Users\John\ProjectXFolder\ProjectXSubFolder
# E.g.: Absolute (abs) path to the Python script: C:\Users\John\ProjectXFolder\ProjectXSubFolder\get_data.py
# get_data.py
import os
script_path = os.path.dirname(os.path.abspath(__file__))
print("script_path: ", script_path)
#returns
C:\Users\John\ProjectXFolder\ProjectXSubFolder
# 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment