Project 4 of 15, for Neil
Recursive File Tree Printer
A file tree printer for Neil's old backup, his first lap timer and his first rockets inside: folders inside folders, each one handled the same way, drawn like the tree command, with every folder's size worked out once and written down.
Sign up free to open itLog in
Act 4 needs a free account, so your work is saved as you go. Act 1 is open to everyone.
The brief
- NeilI've found my old laptop's backup, and it's folders inside folders inside folders.
- NeilMy first kart lap timer's in there, and my first rocket's flight notes, with a lot of typos.
- NeilI'd like to see what's on it without opening anything, and find out how big it all is.
- PaulA folder is files plus smaller folders, and the easiest way to look at it is the way it was packed.
In tree.py, a file tree printer for the backup folder, Neil's old laptop, or any folder you name. List a folder with os.listdir(path), in sorted order, and build the path to each name inside with os.path.join(path, name).
folder_size(path, sizes)returns the bytes of every file under the folder, however deep. Neil's version is right but measures the same folders again and again. Memoise it:sizesis a dictionary of folder totals by path. If the folder is in it, hand that back without looking inside; otherwise work it out, write it in, and hand it back.count_files(path)returns how many files are under the folder, however deep.depth(path)returns how many folders deep it goes: 1 for a folder with no folders inside, otherwise one more than the deepest folder inside it.tree_lines(path, sizes, prefix="")returns a list of lines for everything inside the folder, names in sorted order. Each line isprefix, then├──(or└──for the last name in the folder), thenname/ (204 bytes)for a folder, usingfolder_sizewith the samesizes, orname (70 bytes)for a file. Under each folder come its own lines, withprefixplus│and three spaces, or four spaces if that folder was the last name.
The main block prints the folder named in sys.argv, or backup if none is, with its size on the first line and the files and depth on the last.
backup/ (835 bytes)
├── README.txt (94 bytes)
├── karting/ (204 bytes)
│ └── lap-timer/ (204 bytes)
│ ├── notes.txt (70 bytes)
│ └── versions/ (134 bytes)
│ ├── v1.txt (73 bytes)
│ └── v2.txt (61 bytes)
├── rockets/ (258 bytes)
│ ├── checklist.txt (63 bytes)
│ ├── design.txt (72 bytes)
│ └── flights/ (123 bytes)
│ ├── first.txt (60 bytes)
│ └── second.txt (63 bytes)
├── scripts/ (188 bytes)
│ ├── style/ (116 bytes)
│ │ ├── spaces.txt (92 bytes)
│ │ └── tabs.txt (24 bytes)
│ └── telemetry.txt (72 bytes)
└── teaching/ (91 bytes)
└── duck.txt (91 bytes)
12 files, 4 folders deepWhat you learn
What's in it
- A function that calls itself
- A crate of crates
- Calls that wait
- Up and down again
- The tower of calls
- Where it stops
- Four laps at a time
- Folders inside folders
- Write the answer down
- The board is already written on
- How Paul would remember
- Fix Caitlin's version
- Make the file tree printer
- Ship it