Làm thế nào để bạn xác định 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

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

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

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 cách nào để tạo một thư mục trong Python?

Sử dụng hệ điều hành. phương thức mkdir[] trong Python được sử dụng để tạo một thư mục có tên đường dẫn 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.

Thư mục trong Python là gì?

Thư mục là tập hợp các tệp và thư mục con . Thư mục bên trong thư mục được gọi là thư mục con. Python có mô-đun os cung cấp cho chúng ta nhiều phương thức hữu ích để làm việc với các thư mục [và cả tệp nữa].

Làm thế nào để tạo một thư mục?

Việc tạo thư mục [hoặc thư mục] mới được thực hiện bằng cách sử dụng lệnh "mkdir" [viết tắt của make directory. ] Tôi sẽ tạo một thư mục mới có tên "OtherStuff". Khi tôi gõ "ls", chúng tôi sẽ thấy thư mục mới trong danh sách của chúng tôi. Đó thực sự là tất cả để có nó.

Chủ Đề