Python File Path

less than 1 minute read

Python file path

1. Check directory

Current file name

import os

print(__file__)

directory path

import os

print(os.path.realpath(__file__))

print(os.path.abspath(__file__))

Working file directory path

import os

print(os.path.dirname(os.path.realpath(__file__)))

Working folder directory path

import os

print(os.getcwd())

Working folder directory file list

import os

print(os.listdir(os.getcwd()))


2. Current directory

'./'

3. Parent directory

'../'

4. Change working directory

import os

os.chdir("/home/dada/test/")

Go to top

Leave a comment