How can I get the cursor's position in an ANSI terminal?
<p>I want to get the cursor's position in a terminal window. I know I can <code>echo -e "\033[6n"</code> and <code>read</code> the output <code>-s</code> silently as in <a href="https://stackoverflow.com/a/8353312/4532996">this answer</a>, but how can I do this in Python?</p>

<p>I've tried <a href="https://stackoverflow.com/a/16571630/4532996">this contextmanager</a> like this:</p>

<pre><code>with Capturing() as output:
    sys.stdout.write("\e[6n")
print(output)
</code></pre>

<p>but it only captures the <code>\e[6n</code> (<code>'\x1b[6n'</code>) escape sequence I write, not the <code>^[[x;yR1</code> sequence I need.</p>

<p>I've also tried spawning a <code>subprocess</code> and getting its output, but again, only the escape sequence I write is captured:</p>

<pre><code>output = subprocess.check_output(["echo", "\033[6n"], shell=False)
print(output)
</code></pre>

<p><code>shell=True</code> makes the output be a list of empty strings.</p>

<p>Avoiding <code>curses</code> (because this is supposed to be a <em>simple</em>, poor man's cursor pos getter), how can I get the escape sequence returned by printing <code>\e[6n</code>?</p>