Hướng dẫn python scandir - trăn để leo lên
trừu tượngPEP này đề xuất bao gồm một chức năng lặp thư mục mới, Show
Cơ sở lý luậnThực hiện Cụ thể của đề xuất Ví dụ Ghi chú về bộ nhớ đệm8-9 times as fast on Windows, and about 2-3 times as fast on POSIX systems. So we’re not talking about micro- optimizations. See more benchmarks here. Ghi chú về xử lý ngoại lệ Ủng hộ Thực hiệnCụ thể của đề xuất Ví dụ Cụ thể của đề xuấtos.scandir()Ví dụ scandir(path='.') -> generator of DirEntry objects Ghi chú về bộ nhớ đệm
Sử dụng trong tự nhiên
Tất cả các phương thức có thể thực hiện các cuộc gọi hệ thống trong một số trường hợp và do đó có thể tăng Thuộc tính def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total8 tên phương thức được chọn giống như các thuộc tính trong mô -đun os.walk() 3 mới nếu có thể, cho tính nhất quán. Sự khác biệt duy nhất về chức năng là các phương thức def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total8 đã lưu trữ các giá trị của chúng trên đối tượng nhập sau cuộc gọi đầu tiên. Giống như các hàm khác trong mô -đun def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total2, def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name9 chấp nhận một đối tượng byte hoặc STR cho tham số def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total6 và trả về các thuộc tính os.walk() 8 và os.walk() 9 có cùng loại với def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total6. Tuy nhiên, bạn nên sử dụng loại STR, vì điều này đảm bảo hỗ trợ đa nền tảng cho tên tệp unicode. . os.walk()Là một phần của đề xuất này, def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name9 thay vì os.stat() 3 và os.stat() 4. Điều này sẽ tăng tốc độ os.walk() rất đáng kể (như đã đề cập ở trên, bằng 2-20 lần, tùy thuộc vào hệ thống).Ví dụĐầu tiên, một ví dụ rất đơn giản về def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name9 hiển thị việc sử dụng thuộc tính os.walk() 8 và phương thức os.stat() 8:def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name Hàm Hoặc, để có được tổng kích thước của các tệp trong một cây thư mục, hiển thị việc sử dụng phương thức def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total Điều này cũng cho thấy việc sử dụng tham số it = os.scandir(path) while True: try: entry = next(it) except OSError as error: handle_error(path, error) except StopIteration: break8 thành os.walk() 5 - trong một hàm đệ quy như thế này, có lẽ chúng ta không muốn theo các liên kết. .Lưu ý rằng Ghi chú về bộ nhớ đệmCác đối tượng def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total8 tương đối ngu ngốc - các thuộc tính def get_tree_size(path): """Return total size of files in path and subdirs. If is_dir() or stat() fails, print an error message to stderr and assume zero size (for example, file has been deleted). """ total = 0 for entry in os.scandir(path): try: is_dir = entry.is_dir(follow_symlinks=False) except OSError as error: print('Error calling is_dir():', error, file=sys.stderr) continue if is_dir: total += get_tree_size(entry.path) else: try: total += entry.stat(follow_symlinks=False).st_size except OSError as error: print('Error calling stat():', error, file=sys.stderr) return total7 và def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total6 rõ ràng luôn được lưu trữ và các phương thức os.listdir() 0 và os.listdir() 1 lưu trữ các giá trị của chúng (ngay lập tức trên Windows thông qua hệ thống.Vì lý do này, các đối tượng def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total8 được dự định sẽ được sử dụng và vứt bỏ sau khi lặp lại, không được lưu trữ trong dữ liệu tồn tại lâu dài được cấu trúc và các phương thức được gọi hết lần này đến lần khác. Nếu các nhà phát triển muốn hành vi làm mới của người Viking (ví dụ, để xem thay đổi kích thước tệp), họ chỉ có thể sử dụng các đối tượng Ghi chú về xử lý ngoại lệ
Ví dụ, def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name0 trên các hệ thống đó nếu def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name04 không hỗ trợ def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name05 hoặc trả về def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name05 với giá trị def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name07 điều kiện hoặc trên một số hệ thống tập tin. Thông thường, điều này không quan trọng - ví dụ, Ngoài ra, bởi vì hành vi gây ra ngoại lệ của các phương pháp def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name10 phù hợp với os.walk() 3-chỉ tăng os.walk() 1 trong trường hợp quyền hoặc các lỗi gây tử vong khác, nhưng trả về sai nếu đường dẫn không tồn tại cần thiết để bắt lỗi xung quanh các cuộc gọi def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name13. Tuy nhiên, khi người dùng yêu cầu xử lý lỗi hạt mịn, có thể mong muốn bắt Ví dụ: bên dưới là phiên bản của ví dụ def get_tree_size(path): """Return total size of files in path and subdirs. If is_dir() or stat() fails, print an error message to stderr and assume zero size (for example, file has been deleted). """ total = 0 for entry in os.scandir(path): try: is_dir = entry.is_dir(follow_symlinks=False) except OSError as error: print('Error calling is_dir():', error, file=sys.stderr) continue if is_dir: total += get_tree_size(entry.path) else: try: total += entry.stat(follow_symlinks=False).st_size except OSError as error: print('Error calling stat():', error, file=sys.stderr) return total Ủng hộMô-đun Scandir trên GitHub đã được đưa ra và được sử dụng khá nhiều (xem sử dụng trên mạng hoang dã trong PEP này), nhưng cũng có một chút hỗ trợ trực tiếp cho chức năng giống như scandir từ các nhà phát triển cốt lõi và những người khác trên Python -Dev và Python-Ideas Danh sách gửi thư. Một mẫu:
Hỗ trợ cho bản thân PEP này (hỗ trợ meta?) Đã được Nick Coghlan đưa ra trên Python-dev: Một PEP xem xét tất cả điều này cho 3.5 và đề xuất một API OS.Scandir cụ thể sẽ là một điều tốt. [Nguồn7] Sử dụng trong tự nhiênCho đến nay, việc triển khai def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total3 chắc chắn rất hữu ích, nhưng đã được đánh dấu rõ ràng là Beta Beta, vì vậy, nó không chắc chắn việc sử dụng nó có bao nhiêu trong tự nhiên. Ben Hoyt đã có một số báo cáo từ những người sử dụng nó. Ví dụ:
Những người khác đã yêu cầu một gói PYPI cho nó, đã được tạo ra. Xem gói PYPI. Các số liệu thống kê của GitHub có nghĩa là quá nhiều, nhưng Scandir có một số người theo dõi, vấn đề, dĩa, v.v ... Tại đây, các số liệu thống kê kể từ ngày 7 tháng 7 năm 2014:
Ngoài ra, vì PEP này sẽ tăng tốc độ def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name18 (194.000) như có def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name19 (230.000). Từ chối ý tưởngĐặt tênỨng cử viên thực sự duy nhất khác cho chức năng này Tên tên là def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name20. Tuy nhiên, các chức năng def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name21 trong Python (chủ yếu được tìm thấy trong Python 2) có xu hướng tương đương iterator đơn giản của các đối tác không phải điều khiển của chúng. Ví dụ, def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name22 chỉ là phiên bản ererator của def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name23, nhưng các đối tượng được trả về là giống hệt nhau. Tuy nhiên, trong trường hợp ____ 19 19, các giá trị trả về là các đối tượng khá khác nhau (đối tượng ____28 so với chuỗi tên tệp), vì vậy điều này có thể được phản ánh bởi sự khác biệt về tên - do đó def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name9. Xem một số cuộc thảo luận có liên quan về Python-dev. người hỗ trợ ký đại diện________ 12/________ 13 Trên Windows Hỗ trợ vượt qua một loài ký tự đại diện như def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name29, vì vậy, những người đầu tiên (bao gồm cả tác giả PEP này) cảm thấy sẽ là một ý tưởng tốt để đưa một đối số từ khóa def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name30 cho chức năng def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total3 để người dùng có thể chuyển điều này. Tuy nhiên, theo suy nghĩ và thảo luận thêm, người ta đã quyết định rằng đây sẽ là ý tưởng tồi, trừ khi nó có thể được thực hiện đa nền tảng (một đối số từ khóa def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name32 hoặc tương tự). Điều này có vẻ đủ dễ dàng lúc đầu-chỉ cần sử dụng hỗ trợ đại diện OS trên Windows và một cái gì đó như def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name33 hoặc def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name34 sau đó trên các hệ thống dựa trên POSIX. Thật không may, các quy tắc phù hợp với Wildcard Windows chính xác không thực sự được Microsoft ghi lại ở bất cứ đâu và chúng khá kỳ quặc (xem bài đăng trên blog này), có nghĩa là nó rất có vấn đề khi mô phỏng bằng cách sử dụng def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name33 hoặc regexes. Vì vậy, sự đồng thuận là hỗ trợ Wildcard của Windows là một ý tưởng tồi. Có thể thêm vào một ngày sau đó nếu có một cách đa nền tảng để đạt được nó, nhưng không phải cho phiên bản ban đầu. Đọc thêm về chủ đề Python-Ideas tháng 11 năm 2012 này và chủ đề Python-Dev tháng 6 năm 2014 này trên PEP 471. Các phương thức không theo dõi các liên kết symlink theo mặc địnhCó nhiều cuộc tranh luận về Python-Dev (xem tin nhắn trong chuỗi này) về việc liệu các phương thức def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total8 có nên tuân theo các liên kết tượng trưng hay không (khi các phương thức def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name13 không có tham số it = os.scandir(path) while True: try: entry = next(it) except OSError as error: handle_error(path, error) except StopIteration: break8). Ban đầu họ đã không (xem các phiên bản trước của mô-đun PEP và Scandir.py này), nhưng Victor Stinner đã đưa ra một trường hợp khá hấp dẫn trên Python-dev rằng theo các liên kết symlink theo mặc định là một ý tưởng tốt hơn, bởi vì:
Như một trường hợp cụ thể cho thấy phiên bản không liên kết có liên kết có lỗi, tác giả PEP này đã có lỗi do việc kiểm tra chính xác này sai trong việc triển khai def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name44 ban đầu của anh ấy trong scandir.py (xem vấn đề #4 tại đây). Cuối cùng, không có sự thỏa thuận nào rằng các phương pháp nên tuân theo các liên kết của Symlink, nhưng có sự đồng thuận cơ bản giữa những người tham gia có liên quan nhiều nhất và tác giả PEP này tin rằng trường hợp trên đủ mạnh để đảm bảo theo các liên kết symlink theo mặc định. Ngoài ra, nó rất đơn giản để gọi các phương thức có liên quan với def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name45 nếu hành vi khác là mong muốn. Thuộc tính direntry là thuộc tínhTrong một số cách, nó sẽ đẹp hơn cho def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total8 def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name13 và def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name0 để trở thành thuộc tính thay vì các phương thức, để chỉ ra rằng chúng rất rẻ hoặc miễn phí. Tuy nhiên, đây không phải là trường hợp, vì def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name0 sẽ yêu cầu gọi hệ điều hành trên các hệ thống dựa trên POSIX nhưng không phải trên Windows. Ngay cả os.walk() 5 và bạn bè cũng có thể thực hiện cuộc gọi HĐH trên các hệ thống dựa trên POSIX nếu giá trị def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name51 là def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name07 (trên một số hệ thống tệp nhất định). Ngoài ra, mọi người sẽ mong đợi quyền truy cập thuộc tính def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name53 chỉ tăng def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name54, chứ không phải os.walk() 1 trong trường hợp nó thực hiện một cuộc gọi hệ thống theo bìa. Mã gọi sẽ phải có ____ 156/________ 157 xung quanh những gì trông giống như một quyền truy cập thuộc tính đơn giản, và do đó, nó tốt hơn nhiều để tạo ra các phương thức của chúng.Xem chủ đề Python-dev tháng 5 năm 2013 này, nơi tác giả PEP này đưa ra trường hợp này và có thỏa thuận từ một nhà phát triển cốt lõi. Các trường Direntry là các đối tượng chỉ thuộc tính thuộc tính tĩnhTrong thông điệp Python-Dev tháng 7 năm 2014 này, Paul Moore đã đề xuất một giải pháp là một trình bao bọc mỏng của người Viking, trong đó đối tượng def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total8 chỉ có các thuộc tính tĩnh: def get_tree_size(path): """Return total size of files in path and subdirs. If is_dir() or stat() fails, print an error message to stderr and assume zero size (for example, file has been deleted). """ total = 0 for entry in os.scandir(path): try: is_dir = entry.is_dir(follow_symlinks=False) except OSError as error: print('Error calling is_dir():', error, file=sys.stderr) continue if is_dir: total += get_tree_size(entry.path) else: try: total += entry.stat(follow_symlinks=False).st_size except OSError as error: print('Error calling stat():', error, file=sys.stderr) return total7, def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total6 và os.listdir() 0, với các thuộc tính def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name62 chỉ có trên Windows . Ý tưởng là sử dụng chức năng cấp thấp hơn, đơn giản hơn như một khối xây dựng cho các chức năng cấp cao hơn. Lúc đầu, có thỏa thuận chung đơn giản hóa theo cách này là một điều tốt. Tuy nhiên, có hai vấn đề với phương pháp này. Đầu tiên, giả định là def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name63 và các thuộc tính tương tự luôn có mặt trên POSIX, đó là trường hợp (nếu def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name05 không có mặt hoặc là def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name07). Thứ hai, nó có một API khó sử dụng hơn nhiều trong thực tế, vì ngay cả các thuộc tính def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name63 cũng không có mặt trên POSIX, và sẽ cần phải được kiểm tra với def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name67 và sau đó os.stat() gọi nếu chúng có mặt.Xem phản hồi Python-Dev tháng 7 năm 2014 này từ tác giả PEP này chi tiết tại sao tùy chọn này là một giải pháp không lý tưởng và câu trả lời tiếp theo từ thỏa thuận lồng tiếng cho Paul Moore. Các trường Direntry đang tĩnh với tùy chọn đảm bảo_lstatMột lựa chọn có vẻ đơn giản và hấp dẫn khác đã được Nick Coghlan đề xuất trong thông điệp Python-Dev tháng 6 năm 2014 này: Tạo các thuộc tính def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name10 và def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name70 và điền vào thời gian lặp def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name70, nhưng chỉ khi đối số mới def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name72 được quy định trong cuộc gọi def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name9. Điều này có lợi thế so với những điều trên là bạn có thể dễ dàng nhận được kết quả STAT từ def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name9 nếu bạn cần. Tuy nhiên, nó có nhược điểm nghiêm trọng là xử lý lỗi hạt mịn là lộn xộn, bởi vì ____10 sẽ được gọi (và do đó có khả năng tăng os.walk() 1) trong quá trình lặp, dẫn đến một vòng lặp khá xấu xí, được làm bằng tay:it = os.scandir(path) while True: try: entry = next(it) except OSError as error: handle_error(path, error) except StopIteration: break Hoặc điều đó có nghĩa là def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name9 sẽ phải chấp nhận đối số def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name78 - một hàm để gọi khi xảy ra lỗi def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name0 trong quá trình lặp. Điều này dường như đối với tác giả PEP, không trực tiếp cũng như Pythonic như ________ 156/________ 157 xung quanh một cuộc gọi os.walk() 2.Một nhược điểm khác là def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name5 đầy đủ - các phương thức def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name13 là đủ và thông tin này đã được biết đến. Xem Ben Hoyt sườn tháng 7 năm 2014 Trả lời cuộc thảo luận tóm tắt điều này và chi tiết lý do tại sao ông nghĩ rằng đề xuất PEP 471 ban đầu là một trong những điều đúng đắn. Trả về các giá trị là (tên, stat_result) hai bộ phậnBan đầu, tác giả PEP này đã đề xuất khái niệm này như một hàm gọi là def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name87 mang lại hai bộ đôi (tên, stat_result). Điều này có lợi thế là không có loại mới được giới thiệu. Tuy nhiên, def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name5 chỉ được lấp đầy một phần trên các hệ thống dựa trên POSIX (hầu hết các trường được đặt thành def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name89 và các quirks khác), vì vậy chúng không thực sự là đối tượng def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name5, và điều này sẽ phải được ghi lại kỹ lưỡng khác với os.stat() .Ngoài ra, Python có hỗ trợ tốt cho các đối tượng thích hợp với các thuộc tính và phương thức, tạo ra một API đơn giản và đơn giản hơn hai bộ phận. Nó cũng làm cho các đối tượng def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total8 có thể mở rộng hơn và chứng minh trong tương lai khi các hệ điều hành thêm chức năng và chúng tôi muốn đưa vào điều này vào def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total8. Xem thêm một số cuộc thảo luận trước đây:
Trả về các giá trị bị quá tải các đối tượng Stat_ResultMột cách khác được thảo luận là làm cho các giá trị trả về được quá tải các đối tượng def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name5 với các thuộc tính def get_tree_size(path): """Return total size of files in path and subdirs. If is_dir() or stat() fails, print an error message to stderr and assume zero size (for example, file has been deleted). """ total = 0 for entry in os.scandir(path): try: is_dir = entry.is_dir(follow_symlinks=False) except OSError as error: print('Error calling is_dir():', error, file=sys.stderr) continue if is_dir: total += get_tree_size(entry.path) else: try: total += entry.stat(follow_symlinks=False).st_size except OSError as error: print('Error calling stat():', error, file=sys.stderr) return total7 và def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total6. Tuy nhiên, ngoài việc này là một loại quá tải kỳ lạ (và căng thẳng!) Trả về các giá trị là các đối tượng pathlib.pathVới mô -đun thư viện tiêu chuẩn mới của Antoine Pitrou, def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name9 để trả về các trường hợp os.listdir() 5. Tuy nhiên, các chức năng ____ 95 def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name13 và def subdirs(path): """Yield directory names not starting with '.' under given path.""" for entry in os.scandir(path): if not entry.name.startswith('.') and entry.is_dir(): yield entry.name0 không được lưu trữ rõ ràng, trong khi def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total3 phải lưu trữ chúng theo thiết kế, bởi vì nó (thường) trả về các giá trị từ cuộc gọi hệ thống lặp thư mục ban đầu. Và nếu các trường hợp ____995 được trả về bởi các giá trị thống kê được lưu trong bộ nhớ cache, nhưng các đối tượng Guido Van Rossum đã từ chối rõ ràng Có thể cải thiệnCó nhiều cải tiến có thể mà người ta có thể thực hiện để Scandir, nhưng đây là một danh sách ngắn của một số tác giả PEP này có trong tâm trí:
Thảo luận trước
Bản quyềnTài liệu này đã được đặt trong phạm vi công cộng. |