Get the App
SLTechnology News&Howtos  ›  Development  › 

How to obtain directory contents and create directories by Python programming language

Shulou Source: shulou.com Published: 2022-06-02 23:31:19 10月28日 Update

Python programming language how to get directory content and create a directory, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

How the Python programming language gets the contents of the directory

In Python, you can use the os.listdir () function to get the contents of the specified directory. The prototype is shown below.

Os.listdir (path)

The meaning of the parameters is as follows. Path wants to get the path to the content directory. The following example gets the contents of the current directory.

> import os > > os.listdir (os.getcwd ())

Get the contents of the current directory

['dde.pyd',' license.txt', 'Pythonwin.exe'

'scintilla.dll', 'win32ui.pyd',' win32uiole.pyd'

'pywin']

How to create a directory in Python programming language

You can use the os.mkdir () function to create a directory in Python. The prototype is shown below.

Os.mkdir (path)

The meaning of its parameters is. The path where path wants to create the directory. The following example creates a temp directory under the E:\ book directory.

> import os > os.mkdir ('E:\\ book\\ temp')

Create a directory using os.mkdir

Delete directory

You can use the os.rmdir () function to delete a directory in Python. The prototype is shown below.

Os.rmdir (path)

The meaning of the parameters is as follows. The path to the directory that path wants to delete. The following example deletes the E:\ book\ temp directory.

> import os > os.rmdir ('E:\\ book\\ temp')

To delete a directory, it is important to note that the directory deleted using os.rmdir must be an empty directory, otherwise the function will go wrong.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Directory content function programming language language programming prototype parameter meaning instance path help clear for this article novice more knowledge empty directory industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple Redmi Shulou Tech Info Huawei