Recursive mkdir() system call on Unix

There is not a system call to do it for you, unfortunately. I’m guessing that’s because there isn’t a way to have really well-defined semantics for what should happen in error cases. Should it leave the directories that have already been created? Delete them? What if the deletions fail? And so on… It is pretty … Read more

How to mkdir only if a directory does not already exist?

Try mkdir -p: mkdir -p foo Note that this will also create any intermediate directories that don’t exist; for instance, mkdir -p foo/bar/baz will create directories foo, foo/bar, and foo/bar/baz if they don’t exist. Some implementation like GNU mkdir include mkdir –parents as a more readable alias, but this is not specified in POSIX/Single Unix … Read more

mkdir -p functionality in Python [duplicate]

For Python ≥ 3.5, use pathlib.Path.mkdir: import pathlib pathlib.Path(“/tmp/path/to/desired/directory”).mkdir(parents=True, exist_ok=True) The exist_ok parameter was added in Python 3.5. For Python ≥ 3.2, os.makedirs has an optional third argument exist_ok that, when True, enables the mkdir -p functionality—unless mode is provided and the existing directory has different permissions than the intended ones; in that case, OSError … Read more

How do I use filesystem functions in PHP, using UTF-8 strings?

Just urlencode the string desired as a filename. All characters returned from urlencode are valid in filenames (NTFS/HFS/UNIX), then you can just urldecode the filenames back to UTF-8 (or whatever encoding they were in). Caveats (all apply to the solutions below as well): After url-encoding, the filename must be less that 255 characters (probably bytes). … Read more