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