In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to use the Python module os", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the Python module os!
Os.getcwd ()
Returns the absolute path of 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 win32Type "copyright", "credits" or "license ()" for more information. > > import os > print (os.getcwd ()) C:\ Users\ 28914\ AppData\ Local\ Programs\ Python\ Python37os.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)
Create a single-level directory whose parent directory must exist, otherwise you should create a multi-level directory using os.makedirs ()
> import os > os.mkdir ("test1") os.makedirs (dirpath2\ dirpath3)
Create a multi-level directory
> import os > os.makedirs ("test2\\ test3") os.removedirs (dirpath\ filename)
Delete an empty directory and recursively return it to a higher-level directory. If the upper-level directory is also empty, delete it, and so on. If the lowest-level directory is not empty, an error will be reported.
> import os > os.removedirs ("test2\\ test3") os.remove (filepath)
Delete a file
> import os > os.remove ("test.txt") os.listdir (dirpath)
Returns a list of the names of all files and subfolders in the folder
> > import os > > os.listdir ("C:\") ['$360 Sectionals,'$Recycle.Bin', '360 RecoveryEnvelope,' 360 Safeguarding, '360 SANDBOXY,' 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 within the folder and all its descendants
Each item is a tuple of path information, the first item of the tuple is the absolute path, the second item is the list of folders under the path, and the third item is the list of files under the path.
> import os > for item in os.walk (r "E:\ material"): print (item) ('E:\\ material', ['Game Life', 'emoji package'], ['035Sunrise Tomato] Import Picture .jpg,' code-wallpaper-8.jpg']) ('E:\\ material\\ Game Life', ['Picture'] []) ('E:\\ material\\ Game Life\\ Picture', [], [5ab5c9ea15ce36d380894f9931f33a87e850b18e.jpgimagine, '7af40ad162d9f2d387245a7fa2ec8a136327cc6d.jpgboat,' c64edcc451da81cb9482033c5e66d016082431ff.jpg`) os.rename (oldname,newname)
Rename a file or directory
Os.stat (filepath)
Returns the file attribute 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_mode33206 > > state.st_size4429383os.system (command)
Run a system command, which is equivalent to a cmd command in Windows. If the command is executed successfully, 0 is returned, otherwise 1 is returned.
> import os > os.system ("Notepad.exe") os.popen (command)
The system command is also run, but the execution result is returned, which is obtained through .read ().
> import os > result = os.popen ("ipconfig") > result.read ()'\ nWindows IP configuration\ n\ n\ nEthernet adapter Ethernet:\ n\ nMedia status. . . . . . . . . . . . The media has been disconnected\ nto connect to a specific DNS suffix. . . . . . . :\ n\ nEthernet adapter VirtualBox Host-Only Network:\ n\ nConnect to a specific DNS suffix. . . . . . . :\ nLocal link IPv6 address. . . . . . . . : fe80::61c7:10ea:8fe9:d802%9\ nIPv4 address. . . . . . . . . . . . 192.168.56.1\ nSubnet mask. . . . . . . . . . . . 255.255.255.0\ ndefault gateway. . . . . . . . . . . . . :\ n\ nWireless LAN adapter local connection * 1:\ n\ nMedia status. . . . . . . . . . . . The media has been disconnected\ nto connect to a specific DNS suffix. . . . . . . :\ n\ nWireless LAN adapter local connection * 2:\ n\ nMedia status. . . . . . . . . . . . The media has been disconnected\ nto connect to a specific DNS suffix. . . . . . . :\ n\ nWireless LAN adapter WLAN:\ n\ nConnect to a specific DNS suffix. . . . . . . : lan\ nLocal link IPv6 address. . . . . . . . : fe80::fd93:45be:3220:a352%5\ nIPv4 address. . . . . . . . . . . . : 10.10.10.144\ nSubnet mask. . . . . . . . . . . . 255.255.255.0\ ndefault gateway. . . . . . . . . . . . . : 10.10.10.1\ n\ nEthernet adapter Bluetooth network connection:\ n\ nMedia status. . . . . . . . . . . . The media has been disconnected\ nto connect to a specific DNS suffix. . . . . . . At this point, I believe you have a deeper understanding of "how to use the Python module os". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.