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 l开发者_运维技巧ines 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 KeyError
exception.
Try this instead:
# open file if not yet open
files_dict.setdefault(filename, open(filename,'w'))
files_dict[filename].write("test")
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 setdefault
will return the value assigned to the key.
files_dict.setdefault(filename, open(filename,'w')).write("test")
Shawn Chin's proposed solution does indeed remove the error, but it introduces a subtle bug: setdefault's 2nd parameter is a value, not a function called when needed; therefore, open(filename,'w') is called every time setdefault is called. This results in an additional file handle that may conflict with existing open handles in the same program, and it has a number of consequences discussed in "Effective Python: 90 Specific Ways to Write Better Python, 2nd Edition". The book provides a more sophisticated solution, but something simple like this may suffice:
if filename in files_dict:
file = files_dict[filename]
else:
file = open(filename,'w')
files_dict[filename] = file
file.print("test")
精彩评论