Net-informations.com
SiteMap  | About    

Working with Directories in Python

The OS module in python provides functions for interacting with the operating system. This module contains an interface to many operating system-specific functions to manipulate processes, files, file descriptors, directories and other “low level” features of the OS.

Current Working Directory

The getcwd() returns the path to the current working directory. This is the directory which the OS use to transform a relative file name into an absolute file name.

example

List Directory Contents

The listdir() function returns the content of a directory.

example

Create a new Directory/Folder

The mkdir() method creates a new directory. It returns an error if the parent directory does not exist.

example

The above example create a new directory "Temp" in the current path.

Creating Subdirectories

Deleting an empty Directory/Folder

The rmdir() method will delete an empty directory or folder.

example

Renaming a directory/folder

The os.rename() method can rename a folder from an old name to a new one.

example









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.