Làm thế nào để bạn thay đổi tên của một 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 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

Làm thế nào bạn có thể đổi tên một thư mục?

Bạn đổi tên một thư mục bằng cách di chuyển nó sang một tên khác . Sử dụng lệnh mv để đổi tên thư mục. Bạn cũng có thể sử dụng mv để di chuyển một thư mục đến một vị trí trong thư mục khác. Trong ví dụ này, thư mục cà rốt được di chuyển từ rau củ sang rau củ2 bằng lệnh mv.

Làm cách nào để thay đổi thư mục 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() . 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ố đường dẫn có thể là tuyệt đối hoặc tương đối.

Phương pháp nào được sử dụng để đổi tên tệp hoặc thư mục trong Python?

rename() trong Python dùng để đổi tên tệp hoặc thư mục. Phương pháp này đổi tên tệp/thư mục nguồn thành tệp/thư mục đích được chỉ định.

Đổi tên trong Python là gì?

đổi tên phương thức Python() đổi tên tệp hoặc thư mục src thành dst . Nếu dst là một tệp hoặc thư mục (đã có), OSError sẽ được nâng lên.