[Tutor] odd behavior when renaming a file

Alan Gauld alan.gauld at btinternet.com
Thu May 10 18:56:39 CEST 2012


On 09/05/12 20:26, Joel Goldstick wrote:
> import os
> def pre_process():
>      if os.path.isfile('revelex.csv'):
>          os.rename('revelex.csv', 'revelex.tmp')
>          print "Renamed ok"
>      else:
>          print "Exiting, no revelex.csv file available"
>          exit()
>      out_file = open('revelex.csv', 'w')
>      # etc.

> When I run the code above it works file if run from the file.  But
> when I import it and run it from another file it renames the file but
> then prints "Exiting, no revelex.csv file available"

I don;t know the reason but are you sure you want to open the file that 
you have just renamed?

def pre_process():
       if os.path.isfile('revelex.csv'):
           os.rename('revelex.csv', 'revelex.tmp')
...
       out_file = open('revelex.csv', 'w')
       # etc.

I would expect the open() to fail...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list