Using "with open(filename, 'ab'):" and calling code only if the file is new?

Dave Angel davea at davea.name
Tue Oct 29 22:13:05 EDT 2013


On 29/10/2013 21:42, Joseph L. Casale wrote:

You forgot the attribution line:  "Victor says"
>>     with open(self.full_path, 'r') as input, open(self.output_csv, 'ab') as
>> output:
>>         fieldnames = (...)
>>         csv_writer = DictWriter(output, filednames)
>>         # Call csv_writer.writeheader() if file is new.
>>         csv_writer.writerows(my_dict)
>> 
>> I'm wondering what's the best way of calling writeheader() only if the file is
>> new?
>> 
>> My understanding is that I don't want to use os.path.exist(), since that opens
>> me up to race conditions.
>
> What stops you from checking before and setting a flag?

Like Victor says, that opens him up to race conditions.

Victor:

You need to more completely specify your environment.  Are there
multiple instances of this or similar program running simultaneously? 
If so, you've got lots more problems than a missing or duplicated header
line.  You could get partial lines intermixing between the two outputs.

Chances are if you really need to support more than one program at the
same time, you'll need to use a lower-level open, perhaps from the os
module.  Some form of locking is called for.  And if the data SHOULD be
interleaved, you'll have to arrange it so it gets done in whole number
increments.

-- 
DaveA





More information about the Python-list mailing list