[Tutor] odd behavior when renaming a file

Joel Goldstick joel.goldstick at gmail.com
Thu May 10 23:02:08 CEST 2012


On Thu, May 10, 2012 at 4:18 PM, Dave Angel <d at davea.name> wrote:
> On 05/10/2012 12:56 PM, Alan Gauld wrote:
>> 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...
>>
>
> But he's opening it for WRITE, so it gets created just fine.
>
>
>
> --
>
> DaveA
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

I have to process a csv file from a business partner.  Oddly (?) they
don't quote text fields, and the Title field sometimes contains
commas.  So I wrote some code to count the commas in each line and if
there were too many, I removed the extras and wrote the cleaned up
file to the original filename for the rest of what I have to with that
data


-- 
Joel Goldstick


More information about the Tutor mailing list