current folder path in python

To get the current folder path in Python, you can use the os module as well as the pathlib module. Here are two examples using each module:

main.py
# Using os
import os
current_path = os.getcwd()
print("Current folder path:", current_path)
92 chars
5 lines
main.py
# Using pathlib
from pathlib import Path
current_path = Path().resolve()
print("Current folder path:", current_path)
117 chars
5 lines

Both of these snippets will print out the current folder path. The first one uses the getcwd() method from the os module to get the current working directory, while the second one uses the Path class from the pathlib module to create a POSIX-compliant path to the current directory and then resolves any symlinks or relative paths.

gistlibby LogSnag