Hướng dẫn python select directory - python chọn thư mục

Khi có quá nhiều file trong một chương trình, chúng cần sắp xếp các file trong từng thư mục (directory) để dễ quản lý. Một thư mục có thể chứa các tập tin hoặc các thư mục con (subdirectory). Python cũng hỗ trợ module os để giúp thao tác với thư mục, tập tin dễ dàng hơn.os để giúp thao tác với thư mục, tập tin dễ dàng hơn.

Show

1. Thư mục hiện hành (current directory) trong Python

1.1.Lấy đường dẫn của current directory trong Python Lấy đường dẫn của current directory trong Python

Để lấy đường dẫn của thư mục hiện hành (current directory), chúng ta sử dụng hàm

Path of current directory: C:\python-examples
7 trong module os. Hàm
Path of current directory: C:\python-examples
7 sẽ trả về một string là đường dẫn của thư mục mà chúng ta đang làm việc trong đó.
Path of current directory: C:\python-examples
7
trong module os. Hàm
Path of current directory: C:\python-examples
7
sẽ trả về một string là đường dẫn của thư mục mà chúng ta đang làm việc trong đó.

import os
print("Path of current directory:", os.getcwd())
Kết quả
Path of current directory: C:\python-examples

Trong ví dụ trên, chúng ta đang làm việc với file code Python

Path of current directory: C:\python-examples
9 nằm trong thư mục
import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
0.
Path of current directory: C:\python-examples
9
nằm trong thư mục
import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
0
.

1.2. Thay đổi current directory trong Python

Tùy vào yêu cầu chương trình, chúng ta có thể thay đổi current directory. Module os hỗ trợ hàm

import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
1 để làm việc này.os hỗ trợ hàm
import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
1
để làm việc này.

import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
Kết quả
Path of current directory: C:\python-examples
Path of new current directory: C:\Python\Python310

Trong ví dụ trên, chúng ta đang làm việc với file code Python Path of current directory: C:\python-examples9 nằm trong thư mục import os print("Path of current directory:", os.getcwd()) # change current directory os.chdir('C:\Python\Python310') print("Path of new current directory:", os.getcwd()) 0.

1.2. Thay đổi current directory trong Python

import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
2 giúp lấy danh sách tập tin và thư mục con của một thư mục được chỉ rõ đường dẫn. Nếu đường dẫn không được chỉ rõ thì
import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
2
sẽ lấy danh sách tập tin và thư mục con của current directory.

import os
print("List directories and files of Python310 folder:")
print(os.listdir('C:\Python\Python310'))
Kết quả
List directories and files of Python310 folder:
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python310.dll', 'pythonw.exe', 'Scripts', 'share', 'tcl', 'Tools', 'vcruntime140.dll', 'vcruntime140_1.dll']

Trong ví dụ trên, chúng ta đang làm việc với file code Python

Path of current directory: C:\python-examples
9 nằm trong thư mục
import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
0.
import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
4
. Còn đoạn code bên dưới sẽ list directories và files của current directory.

import os
# current directory is C:\python-examples
print("List directories and files of current directory:")
print(os.listdir())
Kết quả
List directories and files of current directory:
['data.txt', 'example.py', 'gochocit.txt', 'main.py', 'Robot', '__pycache__']

Trong ví dụ trên, chúng ta đang làm việc với file code Python Path of current directory: C:\python-examples9 nằm trong thư mục import os print("Path of current directory:", os.getcwd()) # change current directory os.chdir('C:\Python\Python310') print("Path of new current directory:", os.getcwd()) 0.

1.2. Thay đổi current directory trong Python

import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
5 giúp tạo một thư mục mới với việc cung cấp đường dẫn đầy đủ (full path). Nếu không cung cấp full path thì thư mục mới sẽ được tạo trong current directory.

import os
# create folder hocit in current directory
os.mkdir('hocit')
# create folder hocit in C:\Python\Python310
os.mkdir('C:\Python\Python310\hocit')

Tùy vào yêu cầu chương trình, chúng ta có thể thay đổi current directory. Module os hỗ trợ hàm import os print("Path of current directory:", os.getcwd()) # change current directory os.chdir('C:\Python\Python310') print("Path of new current directory:", os.getcwd()) 1 để làm việc này.

2. Lấy danh sách tập tin và thư mục con của một thư mục

import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
6. Hàm
import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
6
có 2 tham số cơ bản. Tham số 1 là tên (hoặc đường dẫn) của folder hoặc file cũ. Tham số 2 là tên (hoặc đường dẫn) của folder hoặc file mới.

import os
# list directoris and file before rename()
print("List directoris and file before rename():")
print(os.listdir())
# rename folder
os.rename('hocit', 'datait')
# rename file
os.rename('gochocit.txt', 'data.txt')
# list directoris and file after rename()
print("List directoris and file after rename():")
print(os.listdir())
Kết quả
Path of current directory: C:\python-examples
0

Trong ví dụ trên, chúng ta đang làm việc với file code Python Path of current directory: C:\python-examples9 nằm trong thư mục import os print("Path of current directory:", os.getcwd()) # change current directory os.chdir('C:\Python\Python310') print("Path of new current directory:", os.getcwd()) 0.

1.2. Thay đổi current directory trong Python

import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
8.

Path of current directory: C:\python-examples
1
Kết quả
Path of current directory: C:\python-examples
2

Trong ví dụ trên, chúng ta đang làm việc với file code Python

Path of current directory: C:\python-examples
9 nằm trong thư mục
import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
0.
import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
8
chỉ có thể xóa những folder rỗng (empty folder).

1.2. Thay đổi current directory trong Pythonhocit có chứa 1 file data.txt. Lúc này, sử dụng hàm

import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
8 để xóa thì chương trình sẽ báo lỗi.

Path of current directory: C:\python-examples
3
Kết quả
Path of current directory: C:\python-examples
4

Trong ví dụ trên, chúng ta đang làm việc với file code Python

Path of current directory: C:\python-examples
9 nằm trong thư mục
import os
print("Path of current directory:", os.getcwd())
# change current directory
os.chdir('C:\Python\Python310')
print("Path of new current directory:", os.getcwd())
0.
Path of current directory: C:\python-examples
Path of new current directory: C:\Python\Python310
1
trong module shutil.

Path of current directory: C:\python-examples
5
Kết quả
Path of current directory: C:\python-examples
6

  • Trong ví dụ trên, chúng ta đang làm việc với file code Python
    Path of current directory: C:\python-examples
    9 nằm trong thư mục
    import os
    print("Path of current directory:", os.getcwd())
    # change current directory
    os.chdir('C:\Python\Python310')
    print("Path of new current directory:", os.getcwd())
    
    0.
  • 1.2. Thay đổi current directory trong Python
  • Tùy vào yêu cầu chương trình, chúng ta có thể thay đổi current directory. Module os hỗ trợ hàm
    import os
    print("Path of current directory:", os.getcwd())
    # change current directory
    os.chdir('C:\Python\Python310')
    print("Path of new current directory:", os.getcwd())
    
    1 để làm việc này.
  • 2. Lấy danh sách tập tin và thư mục con của một thư mục
  • Hàm
    import os
    print("Path of current directory:", os.getcwd())
    # change current directory
    os.chdir('C:\Python\Python310')
    print("Path of new current directory:", os.getcwd())
    
    2 giúp lấy danh sách tập tin và thư mục con của một thư mục được chỉ rõ đường dẫn. Nếu đường dẫn không được chỉ rõ thì
    import os
    print("Path of current directory:", os.getcwd())
    # change current directory
    os.chdir('C:\Python\Python310')
    print("Path of new current directory:", os.getcwd())
    
    2 sẽ lấy danh sách tập tin và thư mục con của current directory.