Python Directory

In python, a directory is a type of folder which stores the collection of subdirectories and files. Directories are used to separate, organize and...

In this article, you will learn about python directory, get current working directory, creating new directory, listing directory, changing a directory, renaming directory and removing a directory. 

Python Directory

Python Directory

In python, a directory is a type of folder which stores the collection of subdirectories and files. Directories are used to separate, organize and store the files. A directory without a parent is Known as root directory and the way, called Path, gives route to the file through name of the directory and names of the folders which are separated by colons and slashes.

For the management of the directory, python has module and  submodule that provides the functionalities.

Getting Current Working Directory

Current working directory of a program is that directory in which that python program exists. By using the function of module, we can get the present or current working directory.

>>> import os

>>> os.getcwd()

'C:\\Program Files\\PyScripter'

>>> os.getcwdb()

b'C:\\Program Files\\PyScripter'

Example.....

>>> print(os.getcwd())
C:\Program Files\PyScripter

Changing A Directory

By using the method of the module, we can change the current working directory of a program.

We have to supply the new path as a string to this method. And to separate the path elements, we can use both forward-slash or the backward-slash .

>>> os.chdir('C:\\Python33')

>>> print(os.getcwd())

C:\Python33

Creating a new Directory

In python, by using the method of module, we can create or make a new directory. The path of the new directory is supplied to this method. But before making a new directory, we should always check if the directory already exists or not.

>>> import os

>>> os.mkdir('new_dir')  

>>> os.mkdir('D:/new_dir')

Checking if a Path is directory 

In python, we can check whether a path is directory or not before making a new directory by using the and function of the submodule.

path = os.path.join('C:\\','temp')

if os.path.exists(path):

    print(path + ' : exists')

    if os.path.isdir(path):

        print(path + ' : is a directory'

Listing a Directory

By using the function of module, we can list all the files and subdirectories of a directory. This method takes only one argument i.e., path and if no argument is passed to this method then it returns the list of files and subdirectories of Current Working Directory.

>>> print(os.getcwd())

C:\Python33


>>> os.listdir()

['DLLs',

'Doc',

'include',

'Lib',

'libs',

'LICENSE.txt',

'blog.txt',

'python.exe',

'pycharm.exe',

'file.txt',

'Scripts',

'tcl',

'Tools']


>>> os.listdir('G:\\')

['$RECYCLE.BIN',

'Movies',

'Photos',

'Music',

'Web Series',

'System Volume Information']

Renaming a Directory

The name of python directory can be changed by using the method of module. This method takes two parameters i.e, the old name and the new name of the directory.

>>> os.listdir()

['test']

>>> os.rename('test','new_one')

>>> os.listdir()

['new_one']

Removing a Directory

A python directory can be deleted by using the function of module. But we can only remove an empty directory by using this function.

>>> os.listdir()

['new_one', 'old.txt']

>>> os.remove('old.txt')

>>> os.listdir()

['new_one']

>>> os.rmdir('new_one')

>>> os.listdir()

[]

To delete non-empty directory the [rmtree()] function of [shutil] module is used. 

>>> os.listdir()

['test']

>>> os.rmdir('test')

Traceback (most recent call last):

...

OSError: [WinError 145] The directory is not empty: 'test'

>>> import shutil

>>> shutil.rmtree('test')

>>> os.listdir()

[]

Conclusion

Above we have discussed about python directory, get current working directory, creating new directory, listing directory, changing a directory, renaming directory and removing a directory. In python, a directory is a type of folder which stores the collection of subdirectories and files. For the management of the directory, python has module that provides the functionalities.