Hướng dẫn open folder os python - mở thư mục os python

Trong các dự án phần mềm, không thể tránh được việc thao tác với file/folder vì bản chất các tập tin vẫn là loại thông dụng nhất để lưu trữ thông tin, đặc biệt là các file chứa cấu hình hệ thống, cấu hình phần mềm. Các ngôn ngữ lập trình đều cung cấp các thư viện, câu lệnh, các cách thức để thao tác với các đối tượng file và folder. Tuy nhiên, với nhiều lập trình viên (trong đó có cả tôi) thi thoảng vẫn tự tìm kiếm một số thao tác tưởng như đơn giản vô cùng để xử lý tệp tin.

Nội dung chính ShowShow

  • 1. Lấy thông tin đường dẫn đến file/thư mục.
  • 2. Liệt kê danh sách các file/folder bên trong một thư mục.
  • 3. Tạo mới một thư mục
  • 4. Kiểm tra một file/thư mục có tồn tại hay không?
  • 5. Xóa file/folder.
  • 6. Sao chép một file.
  • 7. Đổi tên/Di chyển vị trí một file.
  • Kết luận:

Ví dụ: Lấy đường dẫn tuyệt đối đến file cấu hình để truyền vào các controller thực hiện việc load cấu hình. Bài viết dưới đây sẽ liệt kê một số kỹ thuật hữu ích và thường gặp khi làm việc với file/folder trong Python.
Bài viết dưới đây sẽ liệt kê một số kỹ thuật hữu ích và thường gặp khi làm việc với file/folder trong Python.

1. Lấy thông tin đường dẫn đến file/thư mục.

2. Liệt kê danh sách các file/folder bên trong một thư mục.
Để lấy đường dẫn của thư mục hiện tại, chúng ta có thể tham khảo câu lệnh sau:

import os

current_directory = os.getcwd()print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))


3. Tạo mới một thư mục

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import platform>>> print(platform.system())Windows>>>>>> import oscurrent_directory = os.getcwd()0current_directory = os.getcwd()1current_directory = os.getcwd()2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> print(platform.system())
Windows
>>>
>>> import os
current_directory = os.getcwd()0
current_directory = os.getcwd()1
current_directory = os.getcwd()2

4. Kiểm tra một file/thư mục có tồn tại hay không?

current_directory = os.getcwd()3current_directory = os.getcwd()4Type "help", "copyright", "credits" or "license" for more information.>>> import platformcurrent_directory = os.getcwd()7current_directory = os.getcwd()8>>>>>> import oscurrent_directory = os.getcwd()0current_directory = os.getcwd()1print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))3
current_directory = os.getcwd()4
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
current_directory = os.getcwd()7
current_directory = os.getcwd()8
>>>
>>> import os
current_directory = os.getcwd()0
current_directory = os.getcwd()1
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))3

5. Xóa file/folder.
Với Windows là dấu \ (backward slash, downward slash, and reverse solidus)
Với Linux là dấu / (forward slash, upward slash and solidus)

6. Sao chép một file.

2. Liệt kê danh sách các file/folder bên trong một thư mục.

3. Tạo mới một thư mục

print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))5print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))6print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))7print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))8print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))9Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win320Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win321
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))5
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))6
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))7
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))8
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))9
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win320
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win321

4. Kiểm tra một file/thư mục có tồn tại hay không?
Để giải quyết được việc này, chúng ta sẽ dùng đến một function khác để lấy đường dẫn tuyệt đối của thư mục.

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win323Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win324Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win325Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win326Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win321
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win324
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win325
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win326
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win321

3. Tạo mới một thư mục

4. Kiểm tra một file/thư mục có tồn tại hay không?

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win329print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4Type "help", "copyright", "credits" or "license" for more information.1
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4
Type "help", "copyright", "credits" or "license" for more information.1

5. Xóa file/folder.

Type "help", "copyright", "credits" or "license" for more information.2Type "help", "copyright", "credits" or "license" for more information.3Type "help", "copyright", "credits" or "license" for more information.4Type "help", "copyright", "credits" or "license" for more information.5print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))7Type "help", "copyright", "credits" or "license" for more information.7
Type "help", "copyright", "credits" or "license" for more information.3
Type "help", "copyright", "credits" or "license" for more information.4
Type "help", "copyright", "credits" or "license" for more information.5
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))7
Type "help", "copyright", "credits" or "license" for more information.7

6. Sao chép một file.

Type "help", "copyright", "credits" or "license" for more information.5Type "help", "copyright", "credits" or "license" for more information.9>>> import platform0>>> import platform1
Type "help", "copyright", "credits" or "license" for more information.9
>>> import platform0
>>> import platform1

7. Đổi tên/Di chyển vị trí một file.


>>> import platform2>>> import platform3>>> import platform4>>> import platform5>>> import platform6>>> import platform7
>>> import platform3
>>> import platform4
>>> import platform5
>>> import platform6
>>> import platform7

4. Kiểm tra một file/thư mục có tồn tại hay không?

5. Xóa file/folder.

print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4Type "help", "copyright", "credits" or "license" for more information.1>>> print(platform.system())0>>> print(platform.system())1>>> print(platform.system())2>>> print(platform.system())1>>> print(platform.system())4>>> print(platform.system())5
Type "help", "copyright", "credits" or "license" for more information.1
>>> print(platform.system())0
>>> print(platform.system())1
>>> print(platform.system())2
>>> print(platform.system())1
>>> print(platform.system())4
>>> print(platform.system())5

6. Sao chép một file.


print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))7Type "help", "copyright", "credits" or "license" for more information.7Type "help", "copyright", "credits" or "license" for more information.2>>> print(platform.system())9>>> print(platform.system())1
Type "help", "copyright", "credits" or "license" for more information.7
Type "help", "copyright", "credits" or "license" for more information.2
>>> print(platform.system())9
>>> print(platform.system())1

7. Đổi tên/Di chyển vị trí một file.

print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4Type "help", "copyright", "credits" or "license" for more information.1Windows3>>> print(platform.system())1Windows5>>> print(platform.system())5
Type "help", "copyright", "credits" or "license" for more information.1
Windows3
>>> print(platform.system())1
Windows5
>>> print(platform.system())5

Kết luận:


Ví dụ: Lấy đường dẫn tuyệt đối đến file cấu hình để truyền vào các controller thực hiện việc load cấu hình. Bài viết dưới đây sẽ liệt kê một số kỹ thuật hữu ích và thường gặp khi làm việc với file/folder trong Python.
Type "help", "copyright", "credits" or "license" for more information.1
Windows9
>>> print(platform.system())5
>>>1
>>> print(platform.system())1

Khi làm việc với file/folder, các lập trình viên Python thường sẽ sử dụng lib có sẵn là os và path.Để lấy đường dẫn của thư mục hiện tại, chúng ta có thể tham khảo câu lệnh sau:

>>>3>>> print(platform.system())5>>>5>>> print(platform.system())5
>>> print(platform.system())5
>>>5
>>> print(platform.system())5

5. Xóa file/folder.

6. Sao chép một file.

print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4Type "help", "copyright", "credits" or "license" for more information.1>>>9print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4>>> import os1
Type "help", "copyright", "credits" or "license" for more information.1
>>>9
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4
>>> import os1

>>> import os2print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4>>> import os4
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4
>>> import os4

6. Sao chép một file.

7. Đổi tên/Di chyển vị trí một file.

import os

>>> import os6>>> import os7
>>> import os7

>>> import os8>>> import os9
>>> import os9

Kết luận:
Có nhiều cách sử dụng để sao chép file bàng shutil, mỗi cách có một đặc điểm, kết quả khác nhau về metadata của file, các cách thức được mô tả tại đường dẫn: https://docs.python.org/3/library/shutil.html.args

Thông thường, khi không cần quá chú trọng vào việc metadata, mà chỉ cần copy dữ liệu là đủ, chúng ta chỉ cần dùng lệnh shutil.copyfile

print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4>>> import os4current_directory = os.getcwd()02current_directory = os.getcwd()03current_directory = os.getcwd()04print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4current_directory = os.getcwd()06
>>> import os4
current_directory = os.getcwd()02
current_directory = os.getcwd()03
current_directory = os.getcwd()04
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4
current_directory = os.getcwd()06

7. Đổi tên/Di chyển vị trí một file.

Giống như lệnh mv của bash/shell thì module os hỗ trợ việc rename/move một file bằng cùng một lệnh: os.rename. Như ví dụ dưới đây, thực hiện đổi tên file 20200924.py thành 20200925.py


print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4current_directory = os.getcwd()06current_directory = os.getcwd()09print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4current_directory = os.getcwd()11
current_directory = os.getcwd()06
current_directory = os.getcwd()09
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4
current_directory = os.getcwd()11

Di chuyển file sang thư mục khác.

print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4current_directory = os.getcwd()11current_directory = os.getcwd()14print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))7current_directory = os.getcwd()16current_directory = os.getcwd()17print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4current_directory = os.getcwd()19print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))7current_directory = os.getcwd()21
current_directory = os.getcwd()11
current_directory = os.getcwd()14
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))7
current_directory = os.getcwd()16
current_directory = os.getcwd()17
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))4
current_directory = os.getcwd()19
print("Current directory: '{}', type of {}".format(current_directory, type(current_directory)))7
current_directory = os.getcwd()21

Kết luận:

Trên đây là một số lệnh Python cơ bản để làm việc với file/folder. Tôi hy vọng bài viết này sẽ đóng vai trò như một tài liệu tham khảo nhanh cho các bạn khi cần thiết. Ngoài module current_directory = os.getcwd()22 ra, các bạn cũng có thể sử dụng module current_directory = os.getcwd()23 với những tính năng tương tự.Cảm ơn các bạn đã đọc bài viết.
Cảm ơn các bạn đã đọc bài viết.