os of common Python modules

Keywords: Python DNS Windows network

os.getcwd()

Return the absolute path to the current working directory

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import os
>>> print(os.getcwd())
C:\Users\28914\AppData\Local\Programs\Python\Python37

os.chdir(newpath)

Change the current working directory

>>> import os
>>> print(os.getcwd())
C:\Users\28914\AppData\Local\Programs\Python\Python37
>>> os.chdir("C:\\")
>>> print(os.getcwd())
C:\

os.mkdir(dirpath)

To create a single-level directory, its parent directory must exist, otherwise a multilevel directory should be created using os.makedirs()

>>> import os
>>> os.mkdir("test1")

os.makedirs(dirpath1\dirpath2)

Create a multilevel directory

>>> import os
>>> os.makedirs("test2\\test3")

os.removedirs(dirpath\filename)

Delete the empty directory and recursively to the upper directory, delete it if the upper directory is also empty, and so on, error occurs if the lower directory is not empty

>>> import os
>>> os.removedirs("test2\\test3")

os.remove(filepath)

Delete Files

>>> import os
>>> os.remove("test.txt")

os.listdir(dirpath)

Returns a list of all the files and subfolders in the folder

>>> import os
>>> os.listdir("C:\\")
['$360Section', '$Recycle.Bin', '360RecoveryEnv', '360Safe', '360SANDBOX', 'aow_drv.log', 'bootmgr', 'Config.Msi', 'Documents and Settings', 'hiberfil.sys', 'Intel', 'pagefile.sys', 'Program Files', 'Program Files (x86)', 'ProgramData', 'QMDownload', 'QMProxyAccelGameList.dat', 'Qt', 'Recovery', 'Strawberry', 'swapfile.sys', 'System Volume Information', 'temp', 'Users', 'Windows']

os.walk(dirpath)

Returns a generator that traverses all folders and files in a folder and all its descendants
Each item is a tuple of path information, the first of which is the absolute path, the second is a list of folders under the path, and the third is a list of files under the path.

>>> import os
>>> for item in os.walk(r"E:\Source material"):
    print(item)

('E:\\Source material', ['Game Life', 'Emoticon'], ['035 Dawn Tomatoes Import Map.jpg', 'code-wallpaper-8.jpg'])
('E:\\Source material\\Game Life', ['picture'], [])
('E:\\Source material\\Game Life\\picture', [], ['5ab5c9ea15ce36d380894f9931f33a87e850b18e.jpg', '7af40ad162d9f2d387245a7fa2ec8a136327cc6d.jpg', 'c64edcc451da81cb9482033c5e66d016082431ff.jpg'])

os.rename(oldname,newname)

rename

os.stat(filepath)

Return file property information object

>>> import os
>>>> os.stat("C:\\aow_drv.log")
os.stat_result(st_mode=33206, st_ino=41376821576981610, st_dev=2818738962, st_nlink=1, st_uid=0, st_gid=0, st_size=4429383, st_atime=1556684018, st_mtime=1556684018, st_ctime=1556619281)
>>> state = os.stat("C:\\aow_drv.log")
>>> state.st_mode
33206
>>> state.st_size
4429383

os.system(command)

Run the system command, which is equivalent to the cmd command in Windows. If the command executes successfully, it returns 0, otherwise it returns 1

>>> import os
>>> os.system("Notepad.exe")

os.popen(command)

It also runs the system command, but returns the execution result, which is obtained by.read()

>>> import os
>>> result = os.popen("ipconfig")
>>> result.read()
'\nWindows IP To configure\n\n\n Ethernet Adapter Ethernet:\n\n   Media Status  . . . . . . . . . . . . : Media disconnected\n   Connection specific DNS Suffix . . . . . . . : \n\n ethernet adapter VirtualBox Host-Only Network:\n\n   Connection specific DNS Suffix . . . . . . . : \n   Local Links IPv6 address. . . . . . . . : fe80::61c7:10ea:8fe9:d802%9\n   IPv4 address . . . . . . . . . . . . : 192.168.56.1\n   Subnet Mask  . . . . . . . . . . . . : 255.255.255.0\n   Default Gateway. . . . . . . . . . . . . : \n\n Wireless LAN Adapter Local Connection* 1:\n\n   Media Status  . . . . . . . . . . . . : Media disconnected\n   Connection specific DNS Suffix . . . . . . . : \n\n Wireless LAN Adapter Local Connection* 2:\n\n   Media Status  . . . . . . . . . . . . : Media disconnected\n   Connection specific DNS Suffix . . . . . . . : \n\n Wireless LAN Adapter WLAN:\n\n   Connection specific DNS Suffix . . . . . . . : lan\n   Local Links IPv6 address. . . . . . . . : fe80::fd93:45be:3220:a352%5\n   IPv4 address . . . . . . . . . . . . : 10.10.10.144\n   Subnet Mask  . . . . . . . . . . . . : 255.255.255.0\n   Default Gateway. . . . . . . . . . . . . : 10.10.10.1\n\n Bluetooth Network Connection for Ethernet Adapter:\n\n   Media Status  . . . . . . . . . . . . : Media disconnected\n   Connection specific DNS Suffix . . . . . . . : \n'

To learn more about programming and development and grow with me, please pay attention to my public number "Pineal Warehouse" and share all kinds of resources of house & programmer. Thank you!!!

Posted by tony_l on Wed, 20 Nov 2019 21:12:02 -0800