Trong blog này, tôi sẽ học cho bạn cách đếm tổng số tệp và thư mục bằng python. Chúng tôi sẽ hiển thị số lượng tệp và thư mục trong thư mục cụ thể trong python. Tôi muốn chia sẻ với bạn python lấy tổng số tệp và thư mục từ thư mục của bạn trong python
Nếu bạn muốn lấy tệp và thư mục trong thư mục cụ thể của mình bằng python thì bạn có thể sử dụng ví dụ bên dưới cho python
Hướng dẫn này giúp bạn tìm hiểu cách nhanh chóng và dễ dàng đếm số lượng tệp và tổng số thư mục trên PATH cụ thể theo cách đệ quy trong ứng dụng python
chương trình 2. Chương trình này sử dụng hàm openddir[] và readdir[] để đếm tất cả các tệp trong thư mục.
php
glob[ string $pattern, int $flags = 0 ]1
glob[ string $pattern, int $flags = 0 ]2
readdir[ resource $dir_handle ]4
glob[ string $pattern, int $flags = 0 ]4_______0_______5
readdir[ resource $dir_handle ]7
readdir[ resource $dir_handle ]8
readdir[ resource $dir_handle ]9
glob[ string $pattern, int $flags = 0 ]9
20 files6
20 files3_______9_______3
20 files4
readdir[ resource $dir_handle ]4_______9_______6
opendir[ string $path, resource $context ]0
opendir[ string $path, resource $context ]0
20 files9
scandir[ string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, resource $context ]0
scandir[ string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, resource $context ]1
scandir[ string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, resource $context ]2
20 files3
scandir[ string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, resource $context ]4
scandir[ string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, resource $context ]5
20 files6
scandir[ string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, resource $context ]7
scandir[ string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, resource $context ]1
scandir[ string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, resource $context ]9
20 files0
20 files3
20 files2
scandir[ string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, resource $context ]9
20 files4
20 files5
20 files6
20 files3
readdir[ resource $dir_handle ]4
20 files9
scandir[ string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, resource $context ]1_______39_______1
FilesystemIterator::__construct[ string $path, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS ]2_______2_______9
FilesystemIterator::__construct[ string $path, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS ]4
opendir[ string $path, resource $context ]0
opendir[ string $path, resource $context ]7
opendir[ string $path, resource $context ]7
FilesystemIterator::__construct[ string $path, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS ]8
opendir[ string $path, resource $context ]8
int iterator_count[ $iterator ]0
readdir[ resource $dir_handle ]9
int iterator_count[ $iterator ]2
readdir[ resource $dir_handle ]1
đầu ra.
20 files
hàm scandir[]. Hàm scandir[] được sử dụng để trả về một mảng các tệp và thư mục của thư mục đã chỉ định. Hàm scandir[] liệt kê các tệp và thư mục có trong một đường dẫn đã chỉ định
cú pháp.
scandir[ string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, resource $context ]
chương trình 3. Chương trình này sử dụng hàm scandir[] và count[] để đếm tất cả các tệp trong thư mục.
php
glob[ string $pattern, int $flags = 0 ]1
glob[ string $pattern, int $flags = 0 ]2
glob[ string $pattern, int $flags = 0 ]3
glob[ string $pattern, int $flags = 0 ]4_______0_______5
glob[ string $pattern, int $flags = 0 ]6
glob[ string $pattern, int $flags = 0 ]00
glob[ string $pattern, int $flags = 0 ]01
glob[ string $pattern, int $flags = 0 ]02
glob[ string $pattern, int $flags = 0 ]03
glob[ string $pattern, int $flags = 0 ]3
opendir[ string $path, resource $context ]6
glob[ string $pattern, int $flags = 0 ]00
glob[ string $pattern, int $flags = 0 ]07
glob[ string $pattern, int $flags = 0 ]08
glob[ string $pattern, int $flags = 0 ]4
opendir[ string $path, resource $context ]3_______9_______3_______0_______02
glob[ string $pattern, int $flags = 0 ]13
opendir[ string $path, resource $context ]8
glob[ string $pattern, int $flags = 0 ]08
glob[ string $pattern, int $flags = 0 ]16
readdir[ resource $dir_handle ]1
đầu ra.
20 files
Hàm FilesystemIterator[]. Hệ thống tập tinIterator. Hàm __construct[] được sử dụng để tạo trình lặp hệ thống tệp mới từ đường dẫn
cú pháp.
FilesystemIterator::__construct[ string $path, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS ]
hàm iterator_count[]. Hàm iterator_count[] là SPL được sử dụng để đếm các phần tử của iterator
cú pháp.
int iterator_count[ $iterator ]
chương trình 4. Chương trình này sử dụng hàm FilesystemIterator[] và iterator_count[] để đếm tất cả các tệp trong thư mục