Skip to content

Instantly share code, notes, and snippets.

@xaviergoby
Last active June 14, 2019 20:31
Show Gist options
  • Save xaviergoby/33a2fc701f5c3c2b2383467ccfa4a1a7 to your computer and use it in GitHub Desktop.
Save xaviergoby/33a2fc701f5c3c2b2383467ccfa4a1a7 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] Same as the above but get the name of the script aswell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment