欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)目錄樹生成示例

 更新時(shí)間:2014年03月28日 09:35:00   作者:  
這篇文章主要介紹了python實(shí)現(xiàn)目錄樹生成示例,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import optparse

LOCATION_NONE     = 'NONE'
LOCATION_MID      = 'MID'
LOCATION_MID_GAP  = 'MID_GAP'
LOCATION_TAIL     = 'TAIL'
LOCATION_TAIL_GAP = 'TAIL_GAP'

Notations = {
    LOCATION_NONE: '',
    LOCATION_MID: '├─',
    LOCATION_MID_GAP: '│  ',
    LOCATION_TAIL: '└─',
    LOCATION_TAIL_GAP: '    '
}

class Node(object):
    def __init__(self, name, depth, parent=None, location=LOCATION_NONE):
        self.name = name
        self.depth = depth
        self.parent = parent
        self.location = location
        self.children = []

    def __str__(self):
        sections = [self.name]
        parent = self.has_parent()
        if parent:
            if self.is_tail():
                sections.insert(0, Notations[LOCATION_TAIL])
            else:
                sections.insert(0, Notations[LOCATION_MID])
            self.__insert_gaps(self, sections)
        return ''.join(sections)

    def __insert_gaps(self, node, sections):
        parent = node.has_parent()
        # parent exists and parent's parent is not the root node
        if parent and parent.has_parent():
            if parent.is_tail():
                sections.insert(0, Notations[LOCATION_TAIL_GAP])
            else:
                sections.insert(0, Notations[LOCATION_MID_GAP])
            self.__insert_gaps(parent, sections)

    def has_parent(self):
        return self.parent

    def has_children(self):
        return self.children

    def add_child(self, node):
        self.children.append(node)

    def is_tail(self):
        return self.location == LOCATION_TAIL

class Tree(object):
    def __init__(self):
        self.nodes = []

    def debug_print(self):
        for node in self.nodes:
            print(str(node) + '/')

    def write2file(self, filename):
        try:
            with open(filename, 'w') as fp:
                fp.writelines(str(node) + '/\n'
                              for node in self.nodes)
        except IOError as e:
            print(e)
            return 0
        return 1

    def build(self, path):
        self.__build(path, 0, None, LOCATION_NONE)

    def __build(self, path, depth, parent, location):
        if os.path.isdir(path):
            name = os.path.basename(path)
            node = Node(name, depth, parent, location)
            self.add_node(node)
            if parent:
                parent.add_child(node)

            entries = self.list_folder(path)
            end_index = len(entries) - 1
            for i, entry in enumerate(entries):
                childpath = os.path.join(path, entry)
                location = LOCATION_TAIL if i == end_index else LOCATION_MID
                self.__build(childpath, depth + 1, node, location)

    def list_folder(self, path):
        """Folders only."""
        return [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]
        # for entry in os.listdir(path):
        #     childpath = os.path.join(path, entry)
        #     if os.path.isdir(childpath):
        #         yield entry

    def add_node(self, node):
        self.nodes.append(node)

def _parse_args():
    parser = optparse.OptionParser()
    parser.add_option(
        '-p', '--path', dest='path', action='store', type='string',
        default='./', help='the path to generate the tree [default: %default]')
    parser.add_option(
        '-o', '--out', dest='file', action='store', type='string',
        help='the file to save the result [default: pathname.trees]')
    options, args = parser.parse_args()
    # positional arguments are ignored
    return options

def main():
    options = _parse_args()
    path = options.path
    if not os.path.isdir(path):
        print('%s is not a directory' % path)
        return 2

    if not path or path == './':
        filepath = os.path.realpath(__file__)  # for linux
        path = os.path.dirname(filepath)
    tree = Tree()
    tree.build(path)
    # tree.debug_print()
    if options.file:
        filename = options.file
    else:
        name = os.path.basename(path)
        filename = '%s.trees' % name
    return tree.write2file(filename)

if __name__ == '__main__':
    import sys
    sys.exit(main())

運(yùn)行效果

復(fù)制代碼 代碼如下:

gtest_start/
├─build/
├─lib/
│  └─gtest/
├─output/
│  ├─primer/
│  │  ├─Debug/
│  │  │  ├─lib/
│  │  │  └─obj/
│  │  └─Release/
│  │      ├─lib/
│  │      └─obj/
│  └─thoughts/
│      ├─Debug/
│      │  ├─lib/
│      │  └─obj/
│      └─Release/
│          ├─lib/
│          └─obj/
├─src/
│  ├─primer/
│  └─thoughts/
├─test/
│  ├─primer/
│  └─thoughts/
├─third_party/
│  └─gtest/
└─tools/

相關(guān)文章

  • Python實(shí)現(xiàn)pdf轉(zhuǎn)word詳細(xì)代碼

    Python實(shí)現(xiàn)pdf轉(zhuǎn)word詳細(xì)代碼

    在日常工作中,我們經(jīng)常會(huì)遇到需要將PDF文件轉(zhuǎn)換成Word文件的需求。雖然市面上有許多PDF轉(zhuǎn)Word的工具,但是它們通常需要付費(fèi)或者有轉(zhuǎn)換后的格式問題,這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)pdf轉(zhuǎn)word的相關(guān)資料,需要的朋友可以參考下
    2023-09-09
  • 解決python中無法自動(dòng)補(bǔ)全代碼的問題

    解決python中無法自動(dòng)補(bǔ)全代碼的問題

    今天小編就為大家分享一篇解決python中無法自動(dòng)補(bǔ)全代碼的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • 爬蟲代理的cookie如何生成運(yùn)行

    爬蟲代理的cookie如何生成運(yùn)行

    這篇文章主要介紹了爬蟲代理的cookie如何生成運(yùn)行,幫助大家更好的理解和使用爬蟲,感興趣的朋友可以了解下
    2020-09-09
  • python生成器generator用法實(shí)例分析

    python生成器generator用法實(shí)例分析

    這篇文章主要介紹了python生成器generator用法,實(shí)例分析了python生成器的相關(guān)使用技巧,需要的朋友可以參考下
    2015-06-06
  • Python socket實(shí)現(xiàn)多對(duì)多全雙工通信的方法

    Python socket實(shí)現(xiàn)多對(duì)多全雙工通信的方法

    今天小編就為大家分享一篇Python socket實(shí)現(xiàn)多對(duì)多全雙工通信的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • python語法?之與用戶交互和運(yùn)算符

    python語法?之與用戶交互和運(yùn)算符

    這篇文章主要介紹了python語法?之與用戶交互和運(yùn)算符,用戶交互就是人往計(jì)算機(jī)中input/輸入數(shù)據(jù),計(jì)算機(jī)print/輸出結(jié)果,下文更多相關(guān)內(nèi)容需要的朋友可以參考一下
    2022-04-04
  • tensorflow 實(shí)現(xiàn)數(shù)據(jù)類型轉(zhuǎn)換

    tensorflow 實(shí)現(xiàn)數(shù)據(jù)類型轉(zhuǎn)換

    今天小編就為大家分享一篇tensorflow 實(shí)現(xiàn)數(shù)據(jù)類型轉(zhuǎn)換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python3.8 微信發(fā)送服務(wù)器監(jiān)控報(bào)警消息代碼實(shí)現(xiàn)

    python3.8 微信發(fā)送服務(wù)器監(jiān)控報(bào)警消息代碼實(shí)現(xiàn)

    這篇文章主要介紹了python3.8 微信發(fā)送服務(wù)器監(jiān)控報(bào)警消息代碼實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Python處理PPT文件的實(shí)用知識(shí)點(diǎn)總結(jié)

    Python處理PPT文件的實(shí)用知識(shí)點(diǎn)總結(jié)

    python是一門很強(qiáng)大的語言,因?yàn)橛兄S富的第三方庫,所以可以說Python是無所不能的,下面這篇文章主要給大家介紹了關(guān)于用Python提取PPT中圖片的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • numpy庫reshape用法詳解

    numpy庫reshape用法詳解

    這篇文章主要介紹了numpy庫reshape用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04

最新評(píng)論