-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirTreeToLatex.py
More file actions
31 lines (23 loc) · 832 Bytes
/
Copy pathdirTreeToLatex.py
File metadata and controls
31 lines (23 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
import sys
def print_dirtree(dir_path, level=1):
for entry in sorted(os.listdir(dir_path)):
entry_path = os.path.join(dir_path, entry)
# Escape underscores in the filename
escaped_entry = entry.replace('_', '\\_')
# Print the current entry
print(f".{level} {escaped_entry}.")
# If the entry is a directory, recursively print its content
if os.path.isdir(entry_path):
print_dirtree(entry_path, level+1)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python dirtree.py <directory_path>")
sys.exit(1)
dir_path = sys.argv[1]
if not os.path.isdir(dir_path):
print(f"Error: {dir_path} is not a directory.")
sys.exit(1)
print("\\dirtree{%")
print_dirtree(dir_path)
print("}")