Rapunzel Code Editor
Support forum
Python Tutorials
Python videos

Files and folders: Exercise and Solution

An interactive file browser


You're going to build an interactive file browser! The program starts by listing all files and folders in the current working directory. The first entry in the list is a special entry that corresponds to the parent folder, called '..' (two dots is the standard way to indicate the parent folder); however, this entry is only available if the working directory isn't the root already! Each entry in the list is numbered, starting from 0.

By entering a number, the user can select a file or folder. Depending on what kind of file or folder it is, one of three things can happen:

  • If the user selects a folder or the '..' entry, the working directory is changed to that folder.
  • If the user selects a text file, the content of that file is printed out.
  • If the user selects a binary file, ·the message '[file name] is not a text file' is printed out

Next, the contents of the working directory (which may have changed) are shown again, and the user is prompted to provide new input, and so on, until the user enters quit. If the user enters something other than quit or a number that corresponds to an entry in the list, an informative warning message (such as 'Invalid user input') is printed out.

An interaction could look like this:

Listing contents of /home/sebastiaan/coding
0: ..
1: python

>>> 1
Listing contents of /home/sebastiaan/coding/python
0: ..

>>> quit


  • Path.cwd() returns the working directory
  • os.chdir() changes the working directory
  • Calling Path.read_text() on a binary file results in a UnicodeDecodeError


import os
from pathlib import Path

while True:
  # Build a list of files and folders in the working directory.
  entries = []
  current_folder = Path.cwd()
  # If we're not already in the root, the first entry is the parent folder
  if current_folder.parent != current_folder:
  entries += current_folder.glob('*')
  # Print a numbered list of files and folders
  print(f'Listing contents of {current_folder}')
  for i, path in enumerate(entries):
    print(f'{i}: {}')
  # Get user input. Valid input is the word 'quit' or a number that corresponds
  # to an index in the list of files and folders
  user_input = input('>>> ')
  if user_input == 'quit':
    selection = int(user_input)
  except ValueError:
    print('Invalid user input (please enter a number)')
  if selection >= len(entries):
    print('Invalid user input (invalid number)')
  # If the selected path is a folder, then change the working directory to
  # that folder
  selected_path = Path(entries[selection])
  if selected_path.is_dir():
  # Otherwise, try to read the file contents as if they are text. If this fails
  # print a warning message
    file_contents = selected_path.read_text()
  except UnicodeDecodeError:
    print(f'{} is not a text file')
  # Otherwise print the file contents

You're done with this section!

Continue with Files and folders >>