Paul's Python CoursePPC

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.

About an hour and a half, in 15 steps.

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).

  1. 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: sizes is 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.
  2. count_files(path) returns how many files are under the folder, however deep.
  3. 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.
  4. tree_lines(path, sizes, prefix="") returns a list of lines for everything inside the folder, names in sorted order. Each line is prefix, then ├── (or └── for the last name in the folder), then name/ (204 bytes) for a folder, using folder_size with the same sizes, or name (70 bytes) for a file. Under each folder come its own lines, with prefix plus │ 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.

What it prints
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 deep

What you learn

What's in it

  1. A function that calls itself
  2. A crate of crates
  3. Calls that wait
  4. Up and down again
  5. The tower of calls
  6. Where it stops
  7. Four laps at a time
  8. Folders inside folders
  9. Write the answer down
  10. The board is already written on
  11. How Paul would remember
  12. Fix Caitlin's version
  13. Make the file tree printer
  14. Ship it