#!/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(名前、深さ、親、場所)
self.add_node(node)
ifparent:
parent.add_child(node)
entries = self.list_folder(path)
end_index = len(entries) - 1
for i, enumerate(entries):
childpath = os.path.join(path,entries) )
location = LOCATION_TAIL if i == end_index else LOCATION_MID
self.__build(childpath, Depth + 1, node, location)
def list_folder(self, path):
"""フォルダーのみ。"""
return [d for d in os.listdir(path) if os.path.isdir(os.path.join) (path, d))]
# os.listdir(path) のエントリ:
# childpath = os.path.join(path,entry)
# if os.path.isdir(childpath) :
# 収益エントリ
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='ツリーを生成するパス [デフォルト: %default]')
parser.add_option(
'-o', ' --out', dest='file', action='store', type='string',
help='結果を保存するファイル [デフォルト: pathname.trees]')
オプション、引数= parser.parse_args()
# 位置引数は無視されます
戻り値のオプション
def main():
options = _parse_args()
path = options.path
os.path.isdir(path) でない場合:
print('%s はディレクトリではありません' % パス)
return 2
path または path == './' でない場合:
filepath = os.path.realpath(__file__) # 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())
运行效果
gtest_start/
§─build/
§─lib/
│ └─gtest/
§─output/
│ ├─primer/
│ │ χ─Debug/
│ │ │ ├─lib/
│ │ │ └─obj/
│ │ └─Release/
│ │ ├─lib/
│ │ └─obj /
│ └─考え/
│ til─Debug/
│ │ ├─lib/
│ │ └─obj/
│ └─リリース/
│ っていつ─lib /
│ └─obj/
§─src/
│ §─入門書/
│ └─考察/
§─test/
│ ├─入門書/
│ lux─thoughts/
§─ third_party/
│ └─gtest/
└─tools/

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

heouttemptemptostoreavure ofthewrongdatatypeinapythonarray、yure counteractypeerror.thisduetothearraymodule'sstricttypeeencultionyを使用します

PythonListSarePartOfThestAndardarenot.liestareBuilting-in、versatile、forStoringCollectionsのpythonlistarepart。

theScriptisrunningwithwrongthonversionduetorectRectDefaultEntertersettings.tofixthis:1)CheckthedededefaultHaulthonsionsingpython - versionorpython3-- version.2)usevirtualenvironmentsbycreatingonewiththon3.9-mvenvmyenv、andverixe

PythonArraysSupportVariousoperations:1)SlicingExtractsSubsets、2)Appending/ExtendingAdddesements、3)inSertingSelementSatspecificpositions、4)remvingingDeletesements、5)sorting/verversingsorder、and6)listenionsionsionsionsionscreatenewlistsebasedexistin

numpyarraysAressertialentionsionceivationsefirication-efficientnumericalcomputations andDatamanipulation.theyarecrucialindatascience、mashineelearning、物理学、エンジニアリング、および促進可能性への適用性、scaledatiencyを効率的に、forexample、infinancialanalyyy

UseanArray.ArrayOverAlistinPythonは、Performance-criticalCode.1)homogeneousdata:araysavememorywithpedelements.2)Performance-criticalcode:Araysofterbetterbetterfornumerumerumericaleperations.3)interf

いいえ、notallistoperationSaresuptedbyarrays、andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorintorintorinsertizizing、whosimpactsporformance.2)リスト


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

WebStorm Mac版
便利なJavaScript開発ツール

SublimeText3 中国語版
中国語版、とても使いやすい

ホットトピック









