--Import
import os
--Get the current working directory --This prints the directory you are currently in, not the location of the file
os.getcwd()
print(os.getcwd())
# usr/var/www/lib
--Get the location of the running file
print(__file__)
# usr/var/www/lib/hoge.py
--Get the directory of running files --Use os.path.dirname () --No last slash
print(os.path.dirname(__file__))
# usr/var/www/lib
--Get the file name of the running file (after the last slash) --Use os.path.basename () --No slash in the head
print(os.path.basename(__file__))
# hoge.py
--The above method displays the relative path from the current directory --If you want to display with an absolute path --Use os.path.abspath () --This is also valid in a production environment
print(os.path.abspath(__file__))
# usr/var/www/lib
--If you want to move to the directory that contains the currently running file --Use os.chdir ()
os.chdir(os.path.dirname(os.path.abspath(__file__)))
#
Recommended Posts