Home > python > Create and Delete directories with python

Create and Delete directories with python

While working with files in python we may need to find the current working directory. Or sometime we may need to create a directory or remove a directory in python. This article deals with creating, deleting and finding the current working directory in python. Dealing with files does not require any special module or library but in order to work with directories we need to import a module os. Os is a module in python which provides a portable way of using system dependent functionality. It belongs to the core part of the Python ecosystem.

Getting the current working directory in python

As the name implies this part will help us getting our current working directory. getcwd() is the function available in os which will give us the current working directory. It returns the current working directory of the process.

Syntax

os.getcwd()

Example

import os
path = os.getcwd()
print "The current working directory is %s:" % path

output

your current working directory is /home/codeslehrer/python

os.getcwdb() is another function that can be used to get the current working directory.

Python create directory

Python allows us to create our own directory according to our needs.  mkdir() function in os module is used to create a directory in python. It takes a path parameter i.e. path to the directory we want to create.

Syntax

os.mkdir(“/testDir”)

Example

import os
path = "/temp"
try:
os.mkdir(path)
except OSError:
print ("System failed to create directory %s " % path)
else:
print ("System successfully created the directory %s " % path)

Output

System successfully created the directory /temp

Example

import os
path = "/temp/test"
try:
os.mkdir(path)
except OSError:
print ("System failed to create directory %s " % path)
else:
print ("System successfully created the directory %s " % path)

Output

System failed to create directory /temp/test

The thing to keep in mind is that we cannot create a directory with sub directories using mkdir() function in os. So what then? If we want to create a directory with sub directories. One way is to make single call for each of our sub directory. But python saves us from this and provides another function for this purpose. makedirs() function creates the directory with sub directories.

Example

import os
path = "/temp/test/testdir"
try:
os.mkdirs(path)
except OSError:
print ("System failed to create directory %s " % path)
else:
print ("System successfully created the directory %s " % path)

Output

System successfully created the directory /temp/test/ testdir

mkdirs() function calls mkdir() for each of the sub directory by itself. mkdir() function also accepts a second parameter to give permissions to the directory.

Example

import os
path = "/temp "
access = 0o755 # define the access rights in ocatal
try:
os.mkdir(path, access)
except OSError:
print System failed to create directory %s" % path)
else:
print ("System successfully created the directory %s " % path)

Output

System successfully created the directory /temp

Delete directory in python

Deleting a directory is opposite to creating a directory in python. rmdir() function in os module is used to delete an existing directory.

Syntax

os.rmdir(“/temp”)

Example
import os
try:
os.rmdir(“/temp”)
except OSError:
print "System failed to delete directory %s "
else:
print "System successfully deleted the directory %s "

Output
System successfully created the directory /temp
python create directory

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: