[Tutor] odd behavior when renaming a file

Walter Prins wprins at gmail.com
Wed May 9 21:55:30 CEST 2012


Hi,

On 9 May 2012 20:26, Joel Goldstick <joel.goldstick at gmail.com> 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"
>


Can you post where/how you call this from another file? Anyway, it sounds
like the pre_process() routine is being called twice, somehow.  On the
first call the file is renamed.  Then on the second call, of course the
file is not there anymore (as it's been renamed) and thus it prints the
"Exiting" message.

Best,

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120509/c1b4e987/attachment.html>


More information about the Tutor mailing list