A more elegant way to control overwriting with try-except-else in python? or Can I do better than C-style code?
问题 I have code that makes a folder and places output files in it. I want to use a try-except-else block and an overwrite option, which can be set to True or False, so that in the case where the folder already exists and overwrite is set to false it will just print that the folder already exists, and in all other cases it will just execute without comment. The only solution I've come up with so far looks like this: def function( parameters, overwrite = False ): try: os.makedirs( dir ) except