Skip to content

Instantly share code, notes, and snippets.

@casperisfine
Created June 30, 2020 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save casperisfine/2124f349c6564560df4399f2eadaa8f2 to your computer and use it in GitHub Desktop.
Save casperisfine/2124f349c6564560df4399f2eadaa8f2 to your computer and use it in GitHub Desktop.
Python scandir

This is on OSX:

$ time ruby /tmp/scan.rb .
21953
29899

real	0m3.448s
user	0m0.713s
sys	0m2.712s
$ time ruby /tmp/scan.rb .
21953
29899

real	0m4.728s
user	0m0.747s
sys	0m3.948s

Interestingly Python find a few more files and dirs, I suspect the semantic of Dir.Entry.is_dir() doesn't quite match File.directory?

$ time python3 /tmp/scan.py .
22655
29905

real	0m1.118s
user	0m0.367s
sys	0m0.729s
$ time python3 /tmp/scan.py .
22655
29905

real	0m1.123s
user	0m0.371s
sys	0m0.737s
import os, sys
import timeit
def call(path):
path = os.path.abspath(path)
dirs = []
requirables = []
for entry in walk(path):
if entry.is_dir():
dirs.append(entry.path)
elif entry.name.endswith('.rb'):
requirables.append(entry.path)
return [dirs, requirables]
def walk(absolute_dir_path):
for entry in os.scandir(absolute_dir_path):
if entry.name.startswith('.'):
next
yield entry
if entry.is_dir():
yield from walk(entry.path)
dirs, files = call(sys.argv[1])
print(len(dirs))
print(len(files))
def call(path)
path = File.expand_path(path.to_s).freeze
dirs = []
requirables = []
walk(path) do |absolute_path, is_directory|
if is_directory
dirs << absolute_path
elsif absolute_path.end_with?('.rb')
requirables << absolute_path
end
end
[requirables, dirs]
end
def walk(absolute_dir_path, &block)
Dir.each_child(absolute_dir_path) do |name|
next if name.start_with?('.')
absolute_path = "#{absolute_dir_path}/#{name}"
if File.directory?(absolute_path)
yield absolute_path, true
walk(absolute_path, &block)
else
yield absolute_path, false
end
end
end
files, dirs = call(ARGV.first)
p dirs.size
p files.size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment