Using "with open(filename, 'ab'):" and calling code only if the file is new?
Joseph L. Casale
jcasale at activenetwerx.com
Tue Oct 29 21:42:50 EDT 2013
> 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?
More information about the Python-list
mailing list