Phương pháp nào được sử dụng để thay đổi thư mục trong python?

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

# 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 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

# 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. 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

# 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

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

# 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 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

# 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

Sự 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

Phương thức thư mục trong Python là gì?

Tất cả các tệp và thư mục con bên trong một thư mục có thể được truy xuất bằng phương thức listdir() . Phương thức này nhận vào một đường dẫn và trả về danh sách các thư mục con và tệp trong đường dẫn đó. Nếu không có đường dẫn nào được chỉ định, nó sẽ trả về danh sách các thư mục con và tệp từ thư mục làm việc hiện tại.

Phương pháp nào được sử dụng để tạo thư mục mới trong thư mục hiện tại trong Python?

mkdir() hệ điều hành. Phương thức mkdir() trong Python được sử dụng để tạo đường dẫn có tên thư mục với chế độ số được chỉ định. Phương pháp này tăng FileExistsError nếu thư mục được tạo đã tồn tại.