title: python os.rename module deletes files
body: <p>I've wrote a little program to rename a big bunch of files in python</p>
  
  <pre><code>#!/usr/local/bin/python3
  
  import os
  import glob
  import sys
  
  script, arg1 = sys.argv
  f = open(arg1)
  
  # Create a filelist using glob
  filelist = glob.glob('*.avi')
  filelist.sort()
  
  for filename in filelist:
      print(filename)
      # To keep the file ending
      os.rename(filename, f.readline()[:-1]+filename[-4:])
  </code></pre>
  
  <p>It reads the names out of a txt-file which is given as an argument and renames the other <code>avi</code> files. But there was one file missing with the start number '102' so now the complete order from '102' on is wrong. If I now rerun this file and delete the line 102 in my <code>txt</code> file every file from 102 on is deleted except the last one.<br>
  May you know how to fix this little bug and can explain to me how this happens because I work on this far too long.</p>
  
  <p>---Edit---</p>
  
  <p>A short snippet of my text file</p>
  
  <pre><code>snip
  099. Abenteuer in Ido 
  100. Corsa, der junge Rebell 
  101. Unser Papa ist ein Held 
  102. Die Gesetze des Vertrauens 
  103. Um acht in Spider's Cafe 
  104. Ruffy gegen Vivi 
  105. Aufruhr in Rainbase 
  snip
  </code></pre>
  
  <p>The list goes on up to 400. I've got 300 files so the problem with os.rename won't happen. The file with the number 102 didn't exist when i first ran the script, so everything above is one number wrong. I added the file 102 and want to rerun this script.</p>
  
  <p>The files before I rerun:</p>
  
  <pre><code>...
  100. Corsa, der junge Rebell.avi 
  101. Unser Papa ist ein Held.avi 
  102. Die Gesetze des Vertrauens.avi 
  103. Um acht in Spider's Cafe.avi 
  104. Ruffy gegen Vivi.avi 
  105. Aufruhr in Rainbase.avi
  ... 
  </code></pre>
  
  <p>The files afterwards:</p>
  
  <pre><code>...
  100. Corsa, der junge Rebell.avi 
  101. Unser Papa ist ein Held.avi 
  301. Sogeking, der Held der Stunde.avi
  ... 
  </code></pre>
  
  <p>If you need any more information I will provide it as well as possible</p>