Hướng dẫn cp folder python - thư mục cp 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.

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.

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:
Để 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)))


Với hệ điều hành Windows:

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

Với hệ điều hành Linux (hoặc các *UNIX khác):

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

Điều khác biệt giữa 2 hệ điều hành là dấu phân cách giữa các thư mục, file: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)
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)

Đây là điều cần lưu ý với các lập trình viên khi thực hiện lập trình trên một hệ điều hành thông dụng như Windows và triển khai phần mềm trên một hệ điều hành khác (Ubuntu, CentOS, ...), phần đường dẫn đến file/folder phải có phần tùy chỉnh theo hệ điều hành. 

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

Để lấy danh sách file/folder của thư mục hiện tại, dùng function os.listdir().

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

Câu lệnh Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win322 hoạt động được trên Windows, nếu chuyển sang chạy trên hệ điều hành Linux sẽ báo lỗi tại vị trí '\\' do quy ước đường dẫn giữa 2 hệ điều hành khác nhau. Để 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.
Để 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

Để tạo mới một thư mục, sử dụng function Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win328

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

Để tạo thư mục mới bên trong thư mục 'code', cần sử dụng cách nối tên thư mục như ở trên.

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

Nếu thư mục đã tồn tại mà vẫn tạo lại thư mục, sẽ có lỗi xảy ra

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

--> Khi thực hiện tạo thư mục nên chú ý việc handler lỗi . FileExistsError và một số các lỗi khác liên quan đến File/Folder là subclass của nhóm lỗi OSError (https://docs.python.org/3.3/library/exceptions.html#os-exceptions), nên chúng ta có thể handler cả cụm lỗi.


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

Ở ví dụ dưới đây, trong thư mục hiện tại, chúng ta có thư mục 'code', không có file 'check_exist.py', kết quả kiểm tra như bên dưới

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

Tương tự, kiểm tra thư mục 'new_code_folder' có trong thư mục con 'code' hay không ?


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

Kiểm tra input vào có là folder?

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

Kiểm tra đầu vào có là file?


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

Nếu file/folder không tồn tại thì kết quả trả về là False

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

5. Xóa file/folder.

Ví dụ dưới đây sẽ thực hiện xóa file có tên `20200925.txt` và folder có tên `new_folder`

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.

Trong module os, cung cấp một function có tên popen để thực hiện copy file nhưng function này chạy trên Windows và Linux khác nhau.

import os

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

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

Cá nhân tôi thường không sử dụng function này trong chương trình khi cần thực hiện copy/paste file. Tôi sẽ sử dụng module shutil kết hợp với os khi cần copy đến các đường dẫn sâu hơ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
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.