I need to open some new csv files on the fly, depending on what data the infile contains. These csv files need to have filenames based on this data so they can’t be hard coded.
I’m trying to make a dictionary of {filename,FILENAME.CSV}, and am having trouble with the lines below:
if not os.path.exists(filename):
files_dict[filename] = open(filename,'w')
files_dict[filename].write('Test')
The if statement works fine – it will happily go through the infile creating all the necessary csv files.
It doesn’t like the write statement though:
Traceback (most recent call last):
File "R:\DataTeam\Orange\Landline\Fixed\Websource_Landline_FixedData_SplitIntoAccounts_20110307.py", line 141, in <module>
files_dict[filename].write('Test')
KeyError: 'OBS Fixed 6-65544 - BRICO DEPOT 201005.csv'
Any ideas on how to write to these files that have been successfully created? Or is there a much easier way to do this?
Thanks,
Tony
The problem lies with the fact that if the file already exists, you are not opening the file or assigning the filename as a key in the dict, hence the
KeyErrorexception.Try this instead:
This opens a file in write mode if the filename does not yet exist in the dict, and stores the handler in the dict with the filename as the key. Note that files that already exists but has not yet been assigned to the dict will be overwritten.
If you are only performing a single write, you can combine the lines since
setdefaultwill return the value assigned to the key.