Python Directory Operations


The Python os module lets you do directory and file management. You can list files in a directory, create new directories, remove directories and so on.

In this article you learn how to work with directories right from Python code. Don't forget to import the os module, by typing import os, because all the functions are part of the os module which is installed by default.

Related course: Complete Python Programming Course & Exercises

Fetch the List of Files/Directories in a Directory

The os.listdir() method is used to return a list of the names of the files or folders that the specified folder contains. This list is in alphabetical order. It does not include '.' and '...' '' even if it is in the folder.

Only supported under Unix, Windows.

grammatical

The syntax format of the listdir() method is as follows.

os.listdir(path)

parameters

  • path -- the directory path to be listed

return value

  • Returns a list of files and folders under the specified path.

example

The following example demonstrates the use of the listdir() method.

#! /usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# Open file
path = "/var/www/html/"
dirs = os.listdir( path )

# Export all files and folders
for file in dirs:
   print(file)

Create a directory

In Python you can create directories (create first level directories) using the os.mkdir() function.

The prototype is as follows.

os.mkdir(path)

Its parameter path is the path to create the directory.

For example, to create a hello directory on disk D

>>> import os
>>> os.mkdir('d:\hello')

You can create multi-level directories using the os.makedirs() function. The prototype is as follows.

os.makedirs(path)

Its parameter path is the path to create the directory.

If you create a directory for books on disk D, the directory for books is created in the directory for books

>>> import os
>>>>os.makedirs('d:\books\book')

Delete directory

In Python, you can use the os.rmdir() function to delete directories.

The prototype is as follows.

os.rmdir(path)

Its parameter path is the path of the directory to be deleted.

For example, delete hmm's directory from disk D.

>>> import os
>>> os.rmdir('d:\hmm')

Deleting multi-level directories

You can use the os.removedirs() function in Python to remove multi-level directories.

The prototype is as follows.

os.removdirs(path)

Its parameter path is the path of the multilevel directory to be deleted.

>>> import os
>>> os.removedirs('d:\books\book')

Note

The directory to be deleted must be empty

Deletion of documents

In Python you can use the os.remove() function to delete a file (note that it must be a file).

The prototype is as follows.

os.remov(path)

Its parameter path is the path of the file to be deleted.

If you delete the book.txt file in the book directory under the books directory on disk D

>>> import os

>>>os.remove('d:\books\book\book\book.txt')

iterative directory (computing)

In Python you can use the os.walk() function to traverse the directory.

The prototype is as follows.

os.walk(path)

The parameter path is the directory to be traversed, traversing path, returns an object whose parts are each a triplet ('directory x', [directory list under directory x], files under directory x).

For example.

>>> a=os.walk('d:\\books')
>>> def fun():
for i in a:
    print(i)

>>> fun()
('d:\\books', ['book'], ['aa.txt'])
('d:\books\\book', [ ], [ ])

Determine if it is a directory

In Python you can use the os.path.isdir() function to determine if a path is a directory.

The prototype function is as follows.

os.path.isdir(path)

The parameter path is the path to be judged. If yes, return TRUE, otherwise return FALSE.

Determining whether it is a document or not

In Python you can use the os.path.isfile() function to determine if a path is a file. Its function prototype is shown below.

os.path.isfile(path)

Its parameter PATH is the path to be judged. If yes, return TRUE, otherwise return FALSE.