Python - OS Module

Hello, My name is aiyuechuang.

blog url: https://www.aiyc.top/1925.html

It is possible to automatically perform many operating system tasks. The OS module in Python provides functions for creating and removing a directory (folder), fetching its contents, changing and identifying the current directory, etc.

You first need to import the os module to interact with the underlying operating system. So, import it using the import os statement before using its functions.

Getting Current Working Directory

The getcwd() function confirms returns the current working directory.

Example: Get Current Working Directory

>>> import os
>>> os.getcwd()
'C:\\Python37'

Creating a Directory

We can create a new directory using the os.mkdir() function, as shown below.

Example: Create a Physical Directory

>>> import os
>>> os.mkdir("C:\MyPythonProject")

A new directory corresponding to the path in the string argument of the function will be created. If you open the C:\ drive, then you will see the MyPythonProject folder has been created.

By default, if you don’t specify the whole path in the mkdir() function, it will create the specified directory in the current working directory or drive. The following will create MyPythonProject in the C:\Python37 directory.

Example: Create a Physical Directory

>>> import os
>>> os.getcwd()
'C:\Python37'
>>> os.mkdir("MyPythonProject")

Changing the Current Working Directory

We must first change the current working directory to a newly created one before doing any operations in it. This is done using the chdir() function. The following change current working directory to C:\MyPythonProject.

Example: Change Working Directory

>>> import os
>>> os.chdir("C:\MyPythonProject") # changing current workign directory
>>> os.getcwd()
'C:\MyPythonProject'

You can change the current working directory to a drive. The following makes the C:\ drive as the current working directory.

Example: Change Directory to Drive

>>> os.chdir("C:\\")
>>> os.getcwd()
'C:\\'

In order to set the current directory to the parent directory use ".." as the argument in the chdir() function.

Example: Change CWD to Parent

>>> os.chdir("C:\\MyPythonProject")
>>> os.getcwd()
'C:\\MyPythonProject'
>>> os.chdir("..")
>>> os.getcwd()
'C:\\'

Removing a Directory

The rmdir() function in the OS module removes the specified directory either with an absolute or relative path. Note that, for a directory to be removed, it should be empty.

Example: Remove Directory

>>> import os
>>> os.rmdir("C:\\MyPythonProject")

However, you can not remove the current working directory. To remove it, you must change the current working directory, as shown below.

Example: Remove Directory

>>> import os
>>> os.getcwd()
'C:\\MyPythonProject'
>>> os.rmdir("C:\\MyPythonProject")
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'd:\\MyPythonProject'
>>> os.chdir("..")
>>> os.rmdir("MyPythonProject")

Above, the MyPythonProject will not be removed because it is the current directory. We changed the current working directory to the parent directory using os.chdir("..") and then remove it using the rmdir() function.

List Files and Sub-directories

The listdir() function returns the list of all files and directories in the specified directory.

Example: List Directories

>>> import os
>>> os.listdir("c:\python37")
['DLLs', 'Doc', 'fantasy-1.py', 'fantasy.db', 'fantasy.py', 'frame.py', 
'gridexample.py', 'include', 'Lib', 'libs', 'LICENSE.txt', 'listbox.py', 'NEWS.txt',
'place.py', 'players.db', 'python.exe', 'python3.dll', 'python36.dll', 'pythonw.exe', 
'sclst.py', 'Scripts', 'tcl', 'test.py', 'Tools', 'tooltip.py', 'vcruntime140.dll', 
'virat.jpg', 'virat.py']

If we don’t specify any directory, then list of files and directories in the current working directory will be returned.

Example: List Directories of CWD

>>> import os
>>>os.listdir()
['.config', '.dotnet', 'python']

Learn more about OS modules in Python docs

AI yuecuang launched a tutorial class, including “Python language tutorial class, C++ tutorial class, algorithm/data structure tutorial class, children’s programming, pygame game development”, all one-to-one teaching: one-to-one tutoring + one-to-one q&a + assignment + project practice, etc. QQ, wechat online, response at any time! V: Jiabcdefh

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI悦创|编程1v1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值