Skip to main content

Posts

Showing posts from April 15, 2018

How to create a directory if not exists?

#!/usr/bin/python import os file_path = "/home/projects/filename.txt" dir = os . path . dirname(file_path) if not os . path . isdir( dir ) : os . makedirs( dir ) os.path.dirname returns the directory name of pathname path  Ex: os.path.dirname('/home/projects/filename.txt') will returns the directory name projects os.path.isdir returns True if path is an existing directory The method makedirs() is recursive directory creation function. Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory.