Session Ready
Exercise

Counting files in a directory tree

After the last bad experience with corrupt backups at your company, you decide to rewrite the backup script from scratch in Python. One of the improvements you want to make is to audit the number of files in a subdirectory and count them. You will then ensure the exact same number of files exists in a directory tree before and after the backup. This will create a validation step that was missing in the last script.

Use subprocess.run to pipe the output of the find command to wc -l to print the numbers of files in the directory tree.

Instructions
100 XP
  • Use subprocess.run to run unix command find . -type f -print.
  • Send the output of the find command to the input of wc -l.
  • Decode the bytes to strings.
  • Strip the output of spaces and print.