初始化提交
This commit is contained in:
35
common/templates/python/ls-recursive.py
Normal file
35
common/templates/python/ls-recursive.py
Normal file
@@ -0,0 +1,35 @@
|
||||
try:
|
||||
import os
|
||||
except ImportError:
|
||||
import uos as os
|
||||
|
||||
def listdir(directory):
|
||||
result = set()
|
||||
|
||||
def _listdir(dir_or_file):
|
||||
try:
|
||||
# if its a directory, then it should provide some children.
|
||||
children = os.listdir(dir_or_file)
|
||||
except OSError:
|
||||
# probably a file. run stat() to confirm.
|
||||
os.stat(dir_or_file)
|
||||
result.add(dir_or_file)
|
||||
else:
|
||||
# probably a directory, add to result if empty.
|
||||
if children:
|
||||
# queue the children to be dealt with in next iteration.
|
||||
for child in children:
|
||||
# create the full path.
|
||||
if dir_or_file == '/':
|
||||
next = dir_or_file + child
|
||||
else:
|
||||
next = dir_or_file + '/' + child
|
||||
|
||||
_listdir(next)
|
||||
else:
|
||||
result.add(dir_or_file)
|
||||
|
||||
_listdir(directory)
|
||||
return sorted(result)
|
||||
|
||||
print(listdir('{{&path}}'))
|
||||
Reference in New Issue
Block a user