<!-- markdown css tag --><div class="pinggu_markdown">
<div class="pinggu_markdown__html"><pre class=" language-python"><code class="prism language-python"><span class="token keyword">import</span> os
txt <span class="token operator">=</span> <span class="token triple-quoted-string string">""""""
def print_directory_tree(root_dir, level=0, ignore_dirs=None, ignore_files=None):
"""</span>
<span class="token punctuation">:</span>param root_dir<span class="token punctuation">:</span> 要打印的目录
<span class="token punctuation">:</span>param level<span class="token punctuation">:</span> 用于记录当前的层级,用于控制打印时的缩进
<span class="token punctuation">:</span>param ignore_dirs<span class="token punctuation">:</span> 要忽略的目录
<span class="token punctuation">:</span>param ignore_files<span class="token punctuation">:</span> 要忽略的文件
<span class="token punctuation">:</span><span class="token keyword">return</span><span class="token punctuation">:</span>
<span class="token string">""</span>" <span class="token keyword">global</span> txt
<span class="token keyword">if</span> ignore_files <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
ignore_files <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">if</span> ignore_dirs <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
ignore_dirs <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> item <span class="token keyword">in</span> os<span class="token punctuation">.</span>listdir<span class="token punctuation">(</span>root_dir<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> item <span class="token keyword">in</span> ignore_dirs<span class="token punctuation">:</span>
<span class="token keyword">continue</span>
<span class="token keyword">if</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>isfile<span class="token punctuation">(</span>os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>root_dir<span class="token punctuation">,</span> item<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> item <span class="token operator">not</span> <span class="token keyword">in</span> ignore_files<span class="token punctuation">:</span>
txt <span class="token operator">+=</span> <span class="token string">"| "</span> <span class="token operator">*</span> level <span class="token operator">+</span> <span class="token string">"|-- "</span> <span class="token operator">+</span> item <span class="token operator">+</span> <span class="token string">"\n"</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"| "</span> <span class="token operator">*</span> level <span class="token operator">+</span> <span class="token string">"|-- "</span> <span class="token operator">+</span> item<span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
txt <span class="token operator">+=</span> <span class="token string">"| "</span> <span class="token operator">*</span> level <span class="token operator">+</span> <span class="token string">"|-- "</span> <span class="token operator">+</span> item <span class="token operator">+</span> <span class="token string">"/"</span> <span class="token operator">+</span> <span class="token string">"\n"</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"| "</span> <span class="token operator">*</span> level <span class="token operator">+</span> <span class="token string">"|-- "</span> <span class="token operator">+</span> item <span class="token operator">+</span> <span class="token string">"/"</span><span class="token punctuation">)</span>
print_directory_tree<span class="token punctuation">(</span>os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>root_dir<span class="token punctuation">,</span> item<span class="token punctuation">)</span><span class="token punctuation">,</span> level <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> ignore_dirs<span class="token punctuation">,</span> ignore_files<span class="token punctuation">)</span>
<span class="token comment"># print_directory_tree("./", ignore_dirs=[".git"], ignore_files=["README.md"]) </span>
print_directory_tree<span class="token punctuation">(</span><span class="token string">"./"</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> txt<span class="token punctuation">:</span>
<span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"tmp.txt"</span><span class="token punctuation">,</span> <span class="token string">'w'</span><span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">'utf-8'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
f<span class="token punctuation">.</span>write<span class="token punctuation">(</span>txt<span class="token punctuation">)</span>
</code></pre>
</div>
</div>