How to 'ignore' an error in Python?

Cameron Simpson cs at cskk.id.au
Fri Apr 28 21:32:52 EDT 2023


On 28Apr2023 10:39, Mats Wichmann <mats at wichmann.us> wrote:
>For this specific case, you can use os.makedirs:
>os.makedirs(dirname, exist_ok=True)

I'm not a great fan of makedirs because it will make all the missing 
components, not just the final one. So as an example, if you've got a 
NAS mounted backup area at eg:

     /mnt/nas/backups/the-thing/backups-subdir

and your config file has this mistyped as:

     /mn/nas/backups/the-thing/backups-subdir

and makedirs is used, then it will make the backup area on eg the root 
drive where there's no room. (I'm looking at you, Docker, grr). There 
are plenty of similar situations.

Because of this I usually am prepared to make a missing final component 
with mkdir(), but not a potentially deep path with makedirs().

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list