title: How to make a Python script "tab-complete" directories in terminal?
body: <p>I have a Shell Script, let's say <code>run.sh</code>, which reads a user input from keyboard and then does some specific tasks. For some technical reasons I'm migrating this script to Python, e.g <code>run.py</code>, in order to achieve the exact same goal.</p>
  
  <p>In the <code>run.sh</code> file I ask the user a input, which is typically a file in the file system, so I gave the option of "tab-completing" it and I achieved it simply through the line: </p>
  
  <p><code>read -e -p "Choose a file: " file</code></p>
  
  <p>The <code>-e</code> flag does the job of tab-completing users input. For example, if user's current directory is <code>project</code>, which follows the structure:</p>
  
  <pre><code>project
  -- src
  -- shared
     -- lib
     -- imgs
        -- image.png
  -- include
  -- README.txt
  </code></pre>
  
  <p>and the input file is <code>image.png</code> they could proceed as follow:</p>
  
  <p><code>sh&lt;tab&gt;i&lt;tab&gt;&lt;tab&gt;</code> </p>
  
  <p>the result would be <code>shared/imgs/image.png</code>.</p>
  
  <p>Now how do I achieve it inside a Python script? You may believe there are tons of related questions but I haven't been able to reproduce this exact same result in <code>run.py</code>. </p>
  
  <p>What I have tried so far:</p>
  
  <p><strong>1. Python's <code>os</code> module:</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>import os
  
  os.system("read -e -p 'Choose a file:'")
  </code></pre>
  
  <p>Output: <code>sh: 1: read: Illegal option -e</code></p>
  
  <p><strong>2. Python's <code>subprocess</code> module</strong> </p>
  
  <pre class="lang-py prettyprint-override"><code>import subprocess
  
  subprocess.run(['read', '-e', '-p', 'Choose a file'])
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Traceback (most recent call last):
    File "run.py", line 26, in &lt;module&gt;
      subprocess.run(['read', '-e', '-p', 'Choose a file'])
    File "/usr/lib/python3.7/subprocess.py", line 453, in run
      with Popen(*popenargs, **kwargs) as process:
    File "/usr/lib/python3.7/subprocess.py", line 756, in __init__
      restore_signals, start_new_session)
    File "/usr/lib/python3.7/subprocess.py", line 1499, in _execute_child
      raise child_exception_type(errno_num, err_msg, err_filename)
  FileNotFoundError: [Errno 2] No such file or directory: 'read': 'read'
  </code></pre>
  
  <p><strong>3. Python's <code>readline</code> module</strong> </p>
  
  <pre class="lang-py prettyprint-override"><code>import readline
  
  readline.parse_and_bind("tab:complete")
  file = input("Choose a file: ")
  </code></pre>
  
  <p>This one almost seems to work, but there is one big issue: it completes only the files in user's current directory. If user hit <code>s&lt;tab&gt;</code> then <code>src</code> and <code>shared</code> show up, but if they hit <code>sh&lt;tab&gt;</code> the <code>lib</code>and <code>imgs</code> directory do not show up.</p>
  
  <p>I'd like some elegant and simple way to achieve this, but I am convinced this might be a little more difficult than expected. Are there any other approaches that can solve this problem?    </p>