Список дерева каталогов в Python

Методы выше работают отлично. Если вы используете apache commons (org.apache.commons.math.random), просмотрите RandomData. У него есть метод: nextLong (длинный нижний, длинный верхний)

http://commons.apache.org/math/userguide/random.html

http://commons.apache.org/math/api-1.1/org/apache/commons/math/random/RandomData.html#nextLong (длинный,% 20long)

544
задан smci 22 May 2018 в 00:09
поделиться

5 ответов

Это - способ пересечь каждый файл и каталог в дереве каталогов:

import os

for dirname, dirnames, filenames in os.walk('.'):
    # print path to all subdirectories first.
    for subdirname in dirnames:
        print(os.path.join(dirname, subdirname))

    # print path to all filenames.
    for filename in filenames:
        print(os.path.join(dirname, filename))

    # Advanced usage:
    # editing the 'dirnames' list will stop os.walk() from recursing into there.
    if '.git' in dirnames:
        # don't go into any .git directories.
        dirnames.remove('.git')
600
ответ дан Løiten 22 May 2018 в 00:09
поделиться

Попробуйте это:

import os
for top, dirs, files in os.walk('./'):
    for nm in files:       
        print os.path.join(top, nm)
9
ответ дан paxdiablo 22 May 2018 в 00:09
поделиться
import os

for filename in os.listdir("C:\\temp"):
    print  filename
80
ответ дан Tim Cooper 22 May 2018 в 00:09
поделиться

Вот функция помощника, которую я использую довольно часто:

import os

def listdir_fullpath(d):
    return [os.path.join(d, f) for f in os.listdir(d)]
98
ответ дан giltay 22 May 2018 в 00:09
поделиться

Можно использовать

os.listdir(path)

Для ссылки, и больше функций OS смотрит здесь:

521
ответ дан Matt 22 May 2018 в 00:09
поделиться
Другие вопросы по тегам:

Похожие вопросы: