Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách in đường dẫn đầy đủ của thư mục file hiện tại trong Python?


Để lấy đường dẫn đầy đủ của file hiện tại, bạn dùng hàm os.path.abspath. Nếu chỉ muốn đường dẫn thư mục, hãy gọi os.path.dirname.

Ví dụ

import os
absolute_path = os.path.abspath(__file__)
print("Full path: " + absolute_path)
print("Directory Path: " + os.path.dirname(absolute_path))

Kết quả

Sẽ cho ra:

Full path: C:\python27\test.py
Directory path: C:\python27\