Python Devlopment






Directories



Directories helps us make things more managable.  If there are a large number of files , then related files are placed in different directories. Hence, a directory can be said to be a collectopn of files and subdirectories. The module os in Python enables us to use various mathods to work with directories.

1. mkdir() method

The mkdir() mrethod in Python is used to make a new directories in the current directory. It takes the name of the new directory to be created as an argument.

The syntax is

os.mkdir("newdir")

foe example

import os
# Create a new directory test
os.mkdir("test")

 

2. chdir() method

The chdir() method in python's os module is used to change the current directory. It takes the name of the directory that you want tp make the current directory as an argument.

The syntax is

os.chdir("dir_name")

 

For example

import os
#change the current directory to  "/home/testdir"
os.chdir("/home/testdir")

 

3. chdir() Method

getcwd() method displays the current directory in which we are working.

The syntax is

os.getcwd()

 

for example

import os
os.getcwd()

 

4. rmdir() Method

The rmdir() method in Python is used to remove the directories in the current directory. It takes the name of the directory to be deleted as an argument.

The syntax is

os.rmdir("directory_name")

 

for example

import os
os.rmdir(/tmp/test")
# It will remove the "/tmp/test" directory.

All the contents of in a directory should be deleted before removing the directory.