In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Python foundation of os and data structure is how, 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 gain something.
Today summed up the basis of the next Python, found that there are still a lot of foundation to consolidate, directly put the learning content up.
> import os gets the current path > os.getcwd ()'/ root/test' lists the files under the folder where the current path resides > os.listdir (os.getcwd ()) ['a.pyrogen,' redis_test.sql', 'cmdb_server.txt',' a.sqlforth, 'test.py',' redis_test.txt', 'paramiko.pyc',' cmdb_server.txt.bak', 'paramiko.py' 'requirements_add.txt',' test.txt', 'opsmanage.tar.gz',' test.sql'] returns the current absolute path > os.path.abspath ('.') / root/test' to get the last absolute path of the current path > os.path.abspath ('..)'/ root' decomposes the path into path and file name > os.path.split ('/ root/test/test.py') ('/ root/test') 'test.py') > os.path.split ('.') (','.) Merge paths > os.path.join ('/ root/test','test.py')'/ root/test/test.py' returns the folder portion of the specified path > os.path.dirname ('/ root')'/ 'return the folder of the current path > os.path.dirname (os.getcwd ())' / root' to get the current path And the above can be verified by each other > os.getcwd ()'/ root/test' returns the file name in path > os.path.basename ('/ root/test/test.py') 'test.py' returns the subfolder in path > os.path.basename (' / root/test') 'test' > os.path.basename (' / root/test/')'to get the last modification time of the file or folder > os .path.getmtime ('/ root/test/test.py') 1521193690.4832795 gets the size of the file or folder Note that the part of the folder may not be the true size. Not du-sh similar results > os.path.getsize ('/ root/test/test.py') 29 check whether files or folders exist > os.path.exists ('/ root/test/test.py') True > os.path.exists ('/ root/test/test.py22') False some paths in different operating platforms > os.sep'/ > os.extsep'> os.linesep'\ n'> os.pathsep ':' get the files in the directory > os.listdir (os.getcwd ()) ['dict.py' 'sqlplan.py', 'deploy.pyc',' task_manage.py', 'cron.py',' mysql_manage.py', 'system_manage.pyc',' cmdb.pyc', 'deploy.py',' ansible.pyc', 'index.py',' tuning.ini', 'cron.pyc',' backup.pyc', 'mysql_manage.pyc',' users.py', 'celeryHandle.py',' assets.pyc' '_ _ init__.pyc',' ansible.py','_ _ init__.py', 'task_manage.pyc',' cmdb.py', 'users.pyc',' assets.py', 'system_manage.py',' index.pyc', 'dict.pyc' 'backup.py'] order the list of files in the current directory > lists=os.listdir (os.getcwd ()) sort the list > lists.sort () get the list > print (lists) [' _ _ init__.py','_ init__.pyc', 'ansible.py',' ansible.pyc', 'assets.pyc',' backup.py', 'backup.pyc',' celeryHandle.py', 'cmdb.py' 'cmdb.pyc', 'cron.py',' cron.pyc', 'deploy.py',' deploy.pyc', 'dict.py',' dict.pyc', 'index.py',' index.pyc', 'mysql_manage.py',' mysql_manage.pyc', 'sqlplan.py',' system_manage.py', 'system_manage.pyc',' task_manage.py', 'task_manage.pyc',' tuning.ini' 'users.py',' users.pyc'] sort sorts in ascending order by the keywords of key The input parameter fn of lambda is the element of the lists list, and the last modification time of the file is obtained, so the lists element is sorted according to the file time from smallest to largest. Sort by file modification time > lists.sort (key=lambda fn:os.path.getmtime (os.getcwd () +'/'+ fn)) > print (lists) ['_ init__.py', 'deploy.py',' cron.py', 'ansible.py',' _ init__.pyc', 'cron.pyc',' deploy.pyc', 'ansible.pyc',' assets.py', 'assets.pyc',' celeryHandle.py' 'sqlplan.py', 'tuning.ini',' dict.py', 'dict.pyc',' index.py', 'index.pyc',' task_manage.py', 'task_manage.pyc',' users.py', 'users.pyc',' system_manage.py', 'system_manage.pyc',' cmdb.py', 'cmdb.pyc',' backup.py', 'backup.pyc',' mysql_manage.py' 'mysql_manage.pyc'] gets the extension of the file, if the input is a folder Returns empty > os.path.splitext (os.getcwd ()) ('/ root/OpsManage-master/OpsManage/views','') > os.path.splitext ('/ root/OpsManage-master/OpsManage/views/task_manage.pyc') ('/ root/OpsManage-master/OpsManage/views/task_manage', '.pyc') lists all .py files in the current directory > [x for x in os.listdir ('.') If os.path.isfile (x) and os.path.splitext (x) [1] = '.py'] ['dict.py',' sqlplan.py', 'task_manage.py',' cron.py', 'mysql_manage.py',' deploy.py', 'index.py',' users.py', 'celeryHandle.py',' ansible.py','_ init__.py', 'cmdb.py',' assets.py' 'system_manage.py',' backup.py']
Data structure operation
List operation > header= [1Power2 zip 3] > dat= [3LetWord] list converted to dictionary > dict (zip (header,dat)) {1: 3 zip 2: 2 header,dat 3: 1} run operating system commands, use popen > > cmd='hostname' > os.popen (cmd) > os.popen (cmd). Read () 'dev01\ n' to run operating system commands, using commands This return is richer > import commands > commands.getstatusoutput ('hostname') (0,' dev01') list appended > ll= [> ll.append ('jeanron100') > print (ll) [' axiang ('jeanron100') 1 > > print ll.count (' jeanron1000') 0] to determine whether the list elements exist or not. If it's two lists, The effect is more clear > ll.extend (['jeanron','jianrong']) > print (ll) [' await, 'baked,' cased, 'dashed,' jeanron100', 'jeanron','jianrong'] delete the specified element > ll.remove (' jeanron') > print (ll) ['ABA,' baked, 'cased,' dashed, 'jeanron100'] 'jianrong'] reverse output list elements > ll.reverse () > print (ll) [' jianrong', 'jeanron100',' list > ll.sort () > print (ll) ['jeanron100',' name':'jeanron','age':33] dictionary operation > info= {'name':'jeanron','age':33] 'gender':'male'} > print info.get (' name') jeanron output dictionary key > print info.keys () ['gender',' age', 'name'] > print info.items () [(' gender', 'male'), (' age', 33), ('name',' jeanron')] returns all values in the dictionary in a list > print info.values () ['male', 33,' jeanron'] set operation > > info= {'my' 'name','is','jeanron'} > print info set ([' jeanron', 'is',' my',' name']) > test_info= {'this','is','a','test'} collection intersection > print info&test_info set ([' is']) collection > print info.union (test_info) set (['axiomain' name',' this','is',' jeanron', 'test']) 'my']) merge > print info | test_info set ([' await, 'name',' this', 'is',' jeanron', 'test',' my']) is helpful to you after reading 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.
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.