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

2013-10-30 Thread Neil Cerutti
On 2013-10-30, Victor Hooi wrote: > Hi, > > I have a CSV file that I will repeatedly appending to. > > I'm using the following to open the file: > > with open(self.full_path, 'r') as input, open(self.output_csv, 'ab') as > output: > fieldnames = (...) > csv_writer = DictWriter

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

2013-10-30 Thread Antoon Pardon
Op 30-10-13 02:02, Victor Hooi schreef: > Hi, > > I have a CSV file that I will repeatedly appending to. > > I'm using the following to open the file: > > with open(self.full_path, 'r') as input, open(self.output_csv, 'ab') as > output: > fieldnames = (...) > csv_writer = Di

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

2013-10-29 Thread Victor Hooi
Hi, In theory, it *should* just be our script writing to the output CSV file. However, I wanted it to be robust - e.g. in case somebody spins up two copies of this script running concurrently. I suppose the timing would have to be pretty unlucky to hit a race condition there, right? As in, so

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

2013-10-29 Thread Joseph L. Casale
> Like Victor says, that opens him up to race conditions. Slim chance, it's no more possible than it happening in the time try/except takes to recover an alternative procedure. with open('in_file') as in_file, open('out_file', 'ab') as outfile_file: if os.path.getsize('out_file'): pri

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

2013-10-29 Thread Dave Angel
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.w

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

2013-10-29 Thread Joseph L. Casale
> 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