Adam Funk writes: > On 2016-04-28, Grant Edwards wrote: >> >> Then open the output file before you do the GET. > > I guess I could, but fetching the data actually involves a whole lot > of GET requests (the first one includes cross-references to the URLs > where the rest of the data is found), some BeautifulSoup processing, & > a lot of other processing to produce a big dict, which I then write > out as json using what I think is the best way (output_file is an > instance of pathlib.Path): > > with output_file.open(mode='w', encoding='UTF-8', errors='replace') as f: > json.dump(output, f, sort_keys=True, indent=2)
It doesn't matter how much work it actually is. Make it conceptually a single unit: def get_output(): ... with output_file.open(mode='w', encoding='UTF-8', errors='replace') as f: output = get_output() json.dump(output, f, sort_keys=True, indent=2) >> Or just do os.access("directory/where/you/want/to/open/a/file",os.W_OK) > > That's what I'm doing now, but I prefer to give the user the error > message early on. Then do that early on. -- https://mail.python.org/mailman/listinfo/python-list