Làm cách nào để tìm kiếm tệp có tên cụ thể trong python?
Xây dựng vấn đề. Giả sử chúng ta có một thư mục chứa các thư mục con khác chứa các tệp khác. Làm cách nào để chúng tôi tìm kiếm một tệp cụ thể trong các thư mục con trong tập lệnh Python của chúng tôi? Show
Kịch bản. Chúng tôi có một thư mục mẹ ( import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))1) với các thư mục con ( import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))2, import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))3 và import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))4). Có các tệp trong thư mục/thư mục mẹ cũng như các thư mục con. Chúng tôi chỉ cần tìm các tệp import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))5 chỉ có trong các thư mục con, tôi. e. , import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))6, import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))7 và import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))8 và bỏ qua các tệp có trong thư mục mẹ và bất kỳ tệp nào khác có phần mở rộng khác. Chúng ta nên tiếp cận kịch bản này như thế nào? Hãy xem nhanh cấu trúc thư mục mà chúng ta phải xử lý ________số 8Vấn đề ban đầu có vẻ khó khăn, nhưng nó có thể được giải quyết dễ dàng vì Python cung cấp cho chúng ta nhiều thư viện và mô-đun để xử lý các thư mục, thư mục con và tệp chứa trong chúng. Vì vậy, không chậm trễ hơn nữa, chúng ta hãy đi sâu vào các giải pháp cho câu hỏi quan trọng về sứ mệnh của chúng ta 🖊️Lưu ý quan trọng. Mỗi giải pháp quan tâm đến một vài điểm chính. import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))5 trong trường hợp này) và loại bỏ các tệp khác trong thư mục con? 📹Video hướng dẫnLàm cách nào để chỉ tìm kiếm các tệp cụ thể trong các thư mục con trong Python? Xem video này trên YouTube Phương pháp 1. Sử dụng hệ điều hành. đi bộ + kết thúc + tham giaTóm tắt nhanh các điều kiện tiên quyết
Cách tiếp cận
Mã số import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f)) đầu ra import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))0 Phương pháp 2. Sử dụng hệ điều hành. danh sách dir + hệ điều hành. đường dẫn. isdir + endwithđiều kiện tiên quyết. Chúng tôi đã tìm hiểu về các phương pháp import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))09 và import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))08 trong giải pháp trước. Chúng ta hãy xem nhanh một số phương pháp khác sẽ giúp chúng ta trong phương pháp này
Cách tiếp cận
import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))3 đầu ra import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))0 Phương pháp 3. Sử dụng hệ điều hành. scandir + hệ điều hành. listdir + endwith()Ghi chú. Hệ điều hành. scandir() được giới thiệu trong Python 3. 5 và là một trong những phương thức mới nhất trong Python cho phép chúng ta liệt kê tất cả các tệp trong một thư mục. Phương pháp này không trả về một danh sách; Cách tiếp cận
import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))5 đầu ra import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))6 Phương pháp 4. Sử dụng PathlibCách tiếp cận
import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))1 đầu ra import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))0 Phương pháp 5. Sử dụng toàn cầuMô-đun toàn cục trong Python là một mô-đun rất hiệu quả có một số chức năng tích hợp nhất định giúp chúng tôi có khả năng liệt kê các tệp cụ thể trong một thư mục. quả địa cầu. glob() là một trong số hàm đó cung cấp các ký tự đại diện như “*”, “?”, [ranges] giúp quá trình truy xuất đường dẫn trở nên dễ dàng Cách tiếp cận
import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))7 đầu ra import os root_dir = r"C:\Users\SHUBHAM SAYON\Desktop\Parent" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".csv"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f))0 Sự kết luậnTốt. Chúng tôi đã thảo luận nhiều nhất là năm phương pháp để giải quyết vấn đề đã cho. Tuy nhiên, đây là danh sách các bài báo rất được đề xuất nếu bạn muốn tìm hiểu sâu hơn về các vấn đề như thế này –
Hãy theo dõi để biết thêm các bài viết và thảo luận thú vị. học tập vui vẻ Shubham Sayon Tôi là một người tạo nội dung và Blogger Python chuyên nghiệp. Tôi đã xuất bản nhiều bài báo và tạo các khóa học trong một khoảng thời gian. Hiện tại tôi đang làm việc với tư cách là một freelancer toàn thời gian và tôi có kinh nghiệm trong các lĩnh vực như Python, AWS, DevOps và Networking |