How did I overwrite my whole python program with this command prompt command?
<p>So I was putting in some values into a Python program I have written (what it does is irrelevant):</p>

<blockquote>
  <p>E:\Users\Me\Desktop\Python>idtohex.py</p>
  
  <p>Enter ID: 213467</p>
  
  <p>DB 41 03</p>
</blockquote>

<p>Nice, it works alright. Now at this point I had accidentally copied this string to my clipboard:</p>

<blockquote>
  <p>[Newline here]</p>
  
  <p>E:\Users\Me\Desktop\Python>idtohex.py</p>
</blockquote>

<p>From when I had originally started the program in the command prompt.</p>

<p>Now, because of the newline my program crashes as it couldn't handle the input (oops):</p>

<blockquote>
  <p>ValueError: invalid literal for int() with base 10: ''</p>
</blockquote>

<p>Because of this error I am now returned to the command prompt and the next line from my clipboard is entered, which is trying to run this command:</p>

<blockquote>
  <p>E:\Users\Me\Desktop\Python>idtohex.py</p>
</blockquote>

<p>The command prompt gives me this error so I think it's not a big deal and just get on with it:</p>

<blockquote>
  <p>'E:\Users\Me\Desktop\Python' is not recognized as an internal or external command,</p>
  
  <p>operable program or batch file.</p>
</blockquote>

<p>Now I try to run the program again and my whole program is completely empty.  <a href="http://i.stack.imgur.com/Ej0bs.png" rel="nofollow">Overwritten. 0-bytes on disk</a>. So with that simple mistake I somehow overwrote my whole program and have no way to get it back.</p>

<p>How the <strong><em>hell</em></strong> does this happen within the Windows command prompt?</p>

<p>Before running a command, cmd.exe sets up its standard handles for the child process to inherit. The <code>&gt;</code> operator redirects a file descriptor to a file opened for output, and it truncates an existing file to 0 bytes. It defaults to file descriptor 1 (standard output, or stdout). Since the only output is the error message on file descriptor 2 (standard error, stderr), the resulting file is empty. If you had run <code>E:\Users\Me\Desktop\Python 2&gt;idtohex.py</code> (the space is necessary in this case for parsing), the file would instead get overwritten by the error message.</p>

python windows batch-file command-line cmd