Pythonpath có bao gồm thư mục hiện tại không?

Khi bạn chạy tập lệnh Python, thư mục làm việc hiện tại được đặt thành thư mục mà tập lệnh được thực thi

Mô-đun os python cung cấp một cách di động để tương tác với hệ điều hành. Mô-đun này là một phần của thư viện Python tiêu chuẩn và bao gồm các phương thức tìm và thay đổi thư mục làm việc hiện tại

Lấy thư mục làm việc hiện tại bằng Python

Phương thức getcwd() của mô-đun os trong Python trả về một chuỗi chứa đường dẫn tuyệt đối của thư mục làm việc hiện tại. Chuỗi trả về không bao gồm ký tự gạch chéo ở cuối

os.getcwd()

Để sử dụng các phương thức mô-đun os, bạn phải nhập mô-đun ở đầu tệp

Dưới đây là một ví dụ cho thấy cách in thư mục làm việc hiện tại

# Import the os module
import os

# Get the current working directory
cwd = os.getcwd()

# Print the current working directory
print("Current working directory: {0}".format(cwd))

# Print the type of the returned object
print("os.getcwd() returns an object of type: {0}".format(type(cwd)))

Đầu ra sẽ trông giống như thế này

Current working directory: /home/linuxize/Desktop
os.getcwd() returns an object of type: <class 'str'>

Nếu bạn muốn tìm thư mục chứa tập lệnh, hãy sử dụng os.path.realpath(__file__). Nó sẽ trả về một chuỗi chứa đường dẫn tuyệt đối tới tập lệnh đang chạy

Thay đổi thư mục làm việc hiện tại trong Python

Để thay đổi thư mục làm việc hiện tại trong Python, hãy sử dụng phương thức chdir()

os.getcwd(path)

Phương thức chấp nhận một đối số, đường dẫn đến thư mục mà bạn muốn thay đổi. Đối số

# Import the os module
import os

# Get the current working directory
cwd = os.getcwd()

# Print the current working directory
print("Current working directory: {0}".format(cwd))

# Print the type of the returned object
print("os.getcwd() returns an object of type: {0}".format(type(cwd)))
0 có thể là tuyệt đối hoặc tương đối

Đây là một ví dụ

# Import the os module
import os

# Print the current working directory
print("Current working directory: {0}".format(os.getcwd()))

# Change the current working directory
os.chdir('/tmp')

# Print the current working directory
print("Current working directory: {0}".format(os.getcwd()))

Đầu ra sẽ trông giống như thế này

Current working directory: /home/linuxize/Desktop
Current working directory: /tmp

Đối số được cung cấp cho phương thức chdir() phải là một thư mục; . Nếu thư mục được chỉ định không tồn tại, một ngoại lệ

# Import the os module
import os

# Get the current working directory
cwd = os.getcwd()

# Print the current working directory
print("Current working directory: {0}".format(cwd))

# Print the type of the returned object
print("os.getcwd() returns an object of type: {0}".format(type(cwd)))
3 sẽ được đưa ra. Nếu người dùng mà tập lệnh đang chạy không có các quyền cần thiết, một ngoại lệ
# Import the os module
import os

# Get the current working directory
cwd = os.getcwd()

# Print the current working directory
print("Current working directory: {0}".format(cwd))

# Print the type of the returned object
print("os.getcwd() returns an object of type: {0}".format(type(cwd)))
4 sẽ được đưa ra

# Import the os module
import os

path = '/var/www'

try:
    os.chdir(path)
    print("Current working directory: {0}".format(os.getcwd()))
except FileNotFoundError:
    print("Directory: {0} does not exist".format(path))
except NotADirectoryError:
    print("{0} is not a directory".format(path))
except PermissionError:
    print("You do not have permissions to change to {0}".format(path))

Phần kết luận

Để tìm thư mục làm việc hiện tại trong Python, hãy sử dụng

# Import the os module
import os

# Get the current working directory
cwd = os.getcwd()

# Print the current working directory
print("Current working directory: {0}".format(cwd))

# Print the type of the returned object
print("os.getcwd() returns an object of type: {0}".format(type(cwd)))
5 và để thay đổi thư mục làm việc hiện tại, hãy sử dụng
# Import the os module
import os

# Get the current working directory
cwd = os.getcwd()

# Print the current working directory
print("Current working directory: {0}".format(cwd))

# Print the type of the returned object
print("os.getcwd() returns an object of type: {0}".format(type(cwd)))
6

Thư mục hiện tại có trên đường dẫn Python không?

Để biết bạn hiện đang ở thư mục nào trong python, hãy sử dụng phương thức getcwd(). Cwd dành cho thư mục làm việc hiện tại trong python . Điều này trả về đường dẫn của thư mục python hiện tại dưới dạng một chuỗi trong Python.

Làm cách nào để thêm thư mục vào PYTHONPATH?

Thêm đường dẫn bắt buộc vào tệp, mỗi đường một đường. .
Mở Terminal
gõ mở. bash_profile
Trong tệp văn bản bật lên, hãy thêm dòng này vào cuối. xuất PYTHONPATH=$PYTHONPATH. foo/bar
Lưu tệp, khởi động lại Terminal và bạn đã hoàn tất

Việc sử dụng Pythonpath là gì?

Pythonpath là một biến môi trường được sử dụng để chỉ định vị trí của các thư viện Python . Nó thường được các nhà phát triển sử dụng để đảm bảo rằng mã của họ có thể tìm thấy các thư viện Python cần thiết khi nó được chạy.

Sự khác biệt giữa Pythonpath và Pythonhome là gì?

PYTHONPATH có thể được đặt để trỏ tới các thư mục bổ sung có thư viện riêng trong đó. PYTHONHOME đặt vị trí của các thư viện mặc định .