> For the complete documentation index, see [llms.txt](https://python.dainganxanh.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://python.dainganxanh.com/chuong5/bai-28.-quan-ly-file-va-folder.md).

# Bài 28. Quản lý file và folder

Trong bài này, ta sẽ tìm hiểu về quản lý tệp và thư mục bằng Python, cách tạo một thư mục, đổi tên, liệt kê tất cả các thư mục và làm việc với thư mục.

Python có module os cung cấp cho chúng ta nhiều phương pháp hữu ích để làm việc với folder và file.

## Làm việc với module os

### Xác định thư mục hiện tại

```python
import os

a = os.getcwd()
b = os.getcwdb()
print(a)            # in ra đường dẫn đến thư mục hiện tại (kiểu str)
print(type(a))    # <class 'str'>

print(b)            # in ra đường dẫn đến thư mục hiện tại (kiểu bytes)
print(type(b))    # <class 'bytes'>
```

### Mở thư mục khác (chuyển thư mục làm việc)

```python
import os
os.chdir('C:\\Python')     # ví dụ chuyển sang thư mục Python trong ổ C
```

### Xem nội dung thư mục&#x20;

Để xem trong thư mục có chứa gì ta dùng hàm listdir(). Hàm trả về một list là tên các file và thư mục con có trong thư mục đang làm việc.

```python
import os

noidung = os.listdir()
print(noidung)        # list liệt kê tên file và subfolder
```

### Tạo thư mục mới

```python
import os
os.mkdir('thumucmoi')
```

### Xóa thư mục hoặc file

Sử dụng phương thức remove() để xóa file và rmdir() để xóa folder (chỉ xóa được folder trống).

```python
import os
os.remove('filename.txt')     # xóa file có tên: filename.txt
os.rmdir('foldername')        # xóa folder có tên: foldername
print(os.listdir())
```

Để xóa một thư mục không trống, ta có thể sử dụng phương thức rmtree() thuộc module shutil

```python
import shutil

shutil.rmtree('fodername')
print(os.listdir())
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://python.dainganxanh.com/chuong5/bai-28.-quan-ly-file-va-folder.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
