は以下のツリーを考えてみましょう:(その後、ファイルおよびディレクトリ)最も深いネストされたパス
bin\ [directory]
--- file.ext
bin\a [directory]
--- file.bin
--- file2.bin
--- file3.bin
--- anotherDir\ [directory]
-------------- image.png
-------------- image1.png
-------------- image2.png
-------------- image3.png
-------------- image4.png
bin\b [directory]
--- xyz.etc
--- xyz.etc
--- zyx.etc
--- deepDir\ [directory]
-------------- image.tif
-------------- image1.tif
-------------- deepestDir\ [directory]
------------------------- something.exe
------------------------- app.exe
私は何をしたいことは(今の印刷)の最深利用可能なファイルを取得することであり、そのディレクトリ名の後に。
だから、最も深いです。このディレクトリは考える:
-------------- deepestDir\ [directory]
------------------------- something.exe
------------------------- app.exe
まず、私はへの相対パスを印刷したい:
- app.exeのsomething.exeなど
- そして含む印刷ディレクトリは
bin\b\deepDir\deepestDir
、次の最も深いは次のようになります。
--- deepDir\ [directory]
-------------- image.tif
-------------- image1.tif
ので、印刷:
- image.tif「ビン\になりますディレクトリを含む
- image1.tif
- B \ deepDir 'この場合
[..]などメインディレクトリ.
私はいくつかの可能性を試してみましたが、これに終わっています
# First, collect ALL files:
SEARCH_PATH = "E:\\project\\elohim\\"
for root, dirs, files in os.walk(SEARCH_PATH):
for file in files:
relativePath = os.path.relpath(root, SEARCH_PATH)
if relativePath == ".":
relativePath = ""
print 'File: {}'.format(os.path.join(relativePath, file))
# Then collect DEEPEST subdirectories
subDirs = []
for root, dirs, files in os.walk(SEARCH_PATH):
subDirs.append(os.path.relpath(root, SEARCH_PATH))
subDirs.sort(lambda x, y: cmp(x.count(os.path.sep), y.count(os.path.sep)), reverse=True)
for k in subDirs:
print 'Directory: {}'.format(k)
が実際にそれは私が欲しいものではありません、しかし - それは本当に近い(ディレクトリ内のすべてのファイルについて、その最初の検索してから最も深いサブディレクトリのため)です。
(だから何私が今抱えていることは、たとえばです):
- image.png
- image1.png
- image2.png
- image3.png
- image4.png
- xyz.etc
- xyz.etc
- zyx.etc
- image.tif
- image1.tif
[..]
and then directories:
bin\b\deepDir\deepestDir
bin\b\deepDir
bin\b
bin
その十分にその明確ではない、私はのように最高の私ができるように説明しよう、私に知らせてください。
素晴らしいソリューション。ありがとう、トン。 – Rusco