[Tutor] odd behavior when renaming a file

Peter Otten __peter__ at web.de
Wed May 9 23:21:37 CEST 2012


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.
> 
> if __name__ == '__main__':
>     pre_process()
> 
> 
> 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"

Add 

print os.getcwd() 

to your code, you are probably in the wrong directory.



More information about the Tutor mailing list