Created
June 14, 2019 09:53
-
-
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...)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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