Python Directory and File Management

Python Directory and File Management


In this section we will learn the file management in Python programming. We will learn how to create a file, rename the file and how to work with files in Python.

A directory in Python can be referred as the collection of files.

 

Get current Directory

The getcwd () function is used to get the present working directory in Python in the form of string. The getcwdb () method is used to get the current directory in bytes. We can use these functions by importing the os module that supports functions and methods related to directory.

Consider the following example in which we have imported the os module and used getcwd () and getcwdb () function.

CODE

>>> import os

>>> os.getcwd ()

>>> os.getcwdb ()

OUTPUT

‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32’

b’C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32′

import-os

 

Changing Directory

The current directory can be changed by using the chdir () method. Consider the following example:

CODE

>>> os.chdir (‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32’)

>>> print (os. getcwd ())

OUTPUT

C: \ Users \ admin \ AppData \ Local \ Programs \ Python \ Python35-32

print

 

List Directories and Files

In Python we can get the list of directories that is we can find out that how many files are in the directories by using the listdir () method.

A path is specified in this method and we get the list of subdirectories. If we do not specify the path then this method will return the current working directory. Consider the following example:

CODE

>>> print (os.getcwd ())

>>> os.listdir ()

>>> os.listdir (‘E:\\’)

OUTPUT

C: \ Users \ admin \ AppData \ Local \ Programs \ Python \Python35-32

[‘asd.txt’, ‘DLLs’, ‘Doc’, ‘exp’, ‘exp.txt’, ‘include’, ‘Lib’, ‘libs’, ‘LICENSE.txt’, ‘NEWS.txt’, ‘python.exe’, ‘python3.dll’, ‘python35.dll’, ‘pythonw.exe’, ‘README.txt’, ‘Scripts’, ‘tcl’, ‘Tools’, ‘vcruntime140.dll’]

 [‘$RECYCLE.BIN’, ‘AUD_Win7_8_6.0.1.6804_PV’, ‘AUD_Win7_8_6.0.1.6804_PV.exe’, ‘fatimaa.pptx’, ‘INF_allOS_9.3.0.1021_PV.exe’, ‘INF_allOS_9.3.0.1026_PV’, ‘INF_allOS_9.3.0.1026_PV.exe’, ‘Intel’, ‘Microsoft Office 2010 Professional Plus x86’, ‘msdia80.dll’, ‘PROWin7_64_v17.3’, ‘PROWin7_64_v17.3.exe’, ‘PROWin7_64_v17.4’, ‘PROWin7_64_v17.4.exe’, ‘Sofware’, ‘System Volume Information’, ‘temp’, ‘VS_EXPBSLN_x64_enu.CAB’, ‘VS_EXPBSLN_x64_enu.MSI’]

output

In the above example, when we used the listdir () method without specifying the path, then the subdirectories of the current working directories are listed. In the second listdir () statement we specified the path and got the list accordingly.

 

Making a New Directory

In Python a new directory can be created by using the mkdir () method. If we do not specify the path of the directory then the directory will be created in the current working directory.

Consider the following example in which the directory named abc will be created and then we listed the subdirectories of abc directory:

CODE

>>> os.mkdir (‘abc’)

>>> os.listdir ()

OUTPUT

[‘abc’, ‘asd.txt’, ‘DLLs’, ‘Doc’, ‘exp’, ‘exp.txt’, ‘include’, ‘Lib’, ‘libs’, ‘LICENSE.txt’, ‘NEWS.txt’, ‘python.exe’, ‘python3.dll’, ‘python35.dll’, ‘pythonw.exe’, ‘README.txt’, ‘Scripts’, ‘tcl’, ‘Tools’, ‘vcruntime140.dll’]

os-mkdir

It can be seen in the above example that we did not specify the path when listing the subdirectories and the list is printed of the current directory.

 

Renaming a Directory

The directory in Python can be renamed by using the rename () function. This function has two arguments, the first argument shows the old name and the secong argument specifies the new name of the directory.

Consider the following example:

CODE:

>>> os.rename(‘abc’, ‘newname’)

>>> os.listdir()

 

Removing Directory or File

We can delete a file by using the remove () function. The rmdir () function is used to remove an empty file.

Consider the following example:

CODE

os.remove (‘newname’)

os.rmdir (‘newname’)