I just want to find the absolute path to the directory
os.path.abspath(_file__)
is
C:\python27\\test.py
It even contains files.
C:\\python27\
'
How do I find out the directory path only like this?
When determining the directory where the script is located,
os.path.dirname(os.path.Abspath early can be to extract the directory as
,
To find out the directory you are working on
os.getcwd()
You have to write something.
import os
print("dirname:\t" + os.path.dirname(os.path.abspath(__file__)))
print("getcwd:\t\t" + os.getcwd())
Output:
dirname: /Users/username/PycharmProjects/tempdir
getcwd: /Users/username/PycharmProjects/tempdir
import sys, os
print(sys.path) # System path
print(os.getcwd()) #
in the current directoryprint(os.path.realpath(_file_)#File
print(os.path.dirname(os.path.realpath(_file_)))#directory where the file is located
© 2024 OneMinuteCode. All rights reserved.