In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what modules does Python have". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Os module-File operating system:
Os, people who often install systems may often say, os images, xxxx.os files, etc., in fact, os means Operating System, that is, the operating system, since it is an operating system, operating files, it should be similar to node's fs, anyway, let's take a look.
Since it is an operating system and files, there should be basic operations such as new, modify, file path, open, close, read file, etc., as well as file permissions, file renaming and so on, just like fs.read, fs.close, fs.path in node. And the os module in Python, these operations are also available, it is also os.close,os.read,os.mkdir,os.open,os.rename and so on. Don't you find it looks a lot like JavaScript?
Def test_os (self): print ('os_start') print (' current absolute path is:'+ os.path.abspath ('. /') path = os.path.abspath ('. /') if not os.path.exists (path +'/ hello'): # determine whether the path exists os.mkdir (path +'/ hello') # build path # similar to try catch in JavaScript, catch exception try: # build file This method is the default file creation method. For example, txt is the encoding format of gbk. If you need to save it to another encoding format, You can use the codecs module to create the file # f = open (path +'/ hello/test.txt', 'w') f = codecs.open (path +'/ hello/test.txt', 'walled, 'utf-8') # create the file f.write (' hello world, hello) \ n') f.close () except Exception as error: print ('create and write file error:' + str (error)) print ('current working directory is:' + os.getcwd ()) # file rename-- rename / renames os.rename (path +'/ hello/test.txt', path +'/ hello/hello.txt') # rename the previous test.txt file to hello.txt file print ('os_end')
2. Sys module-system instructions and information:
Post it directly, mainly to read information.
Def test_sys (self): print (sys.argv) # gets the parameters of the command line. For example, if we have just executed python. / package.py, it will be returned in the result list. [program name, argv0,1,2 .] # print (sys.modules) # currently introduced module information print (sys.platform) # operating platform information print (sys.version) # python version information print (sys.copyright) # python copyright information # print (sys.getswitchinterval ()) # thread switching interval # print (sys.thread_info) # current thread information # print (sys.stdin) # input related # print (sys.stdout) # output related # print (sys.stderr) # error related #.
3. Requests module-http communication:
Since requests sends http requests and handles communications, according to our general understanding of http or, more generally, Tcp, since it is a request, there are get, post, put and delete, and in fact, requests.get,requests.post,requests.put,requests.delete, and of course, there is a comprehensive requests.request that passes get, post, and other types as parameters. And, attention, this is backstage, backstage! Say the important thing three times, you no longer have to manage cross-domain, there is no cross-domain. I remember one time when I was chatting that the front-end interface could not be adjusted, I mentioned to him that the front-end interface could not be adjusted, and then once he couldn't get through in the background. He also thought it was cross-domain. I used nginx, and he also made a nginx.
Def test_requests (self): def getData (): resp = requests.get ('https://www.imooc.com/article/getarticlelist?marking=fe&page=4') return resp.content def requestData (): resp = requests.request (' GET', 'https://www.imooc.com/article/getarticlelist?marking=fe&page=4') return resp.content # requests.get # requests.post # requests.put # requests.delete # requests.request # in addition, it also has Ignore # requests.head # requests.patch print (getData ()) print (requestData ()) for the time being
4. Thread and threading modules-thread management:
Python manages threads through thread and threading, and imports modules first.
Import _ thread # thread conflicts with threading. Python3 adds a _ private prefix based on the original, which does not affect the use of import threading # threading more advanced, or can replace the old thread. We can just use threading on a daily basis.
See how it works:
Def test_threading (self): # Open multiple threads def run (): print ('the currently executing thread is:' + str (threading.current_thread () time.sleep (2) thread_list = [] I = 5 while I > 0: t = threading.Thread (target=run) thread_list.append (t) I-= 1 for t in thread_list: t.start ()
5. Time, datetime and calendar modules-Calendar, time:
When you see the thread management code above, the familiar sleep function comes from the time module. There's nothing to say about getting the time, but the formatting that comes with it is easier to use, and there's no need to getFullYear () or implement formatting separately, as JavaScript does.
Def test_time (self): print (time.time ()) # timestamp print (time.localtime ()) # Local time print (time.strftime ("a% b% d% H:%M:%S% Y", time.localtime ()) # Local time, formatted into print (time.strftime ("% Y-%m-%d% H:%M:%S", time.localtime () # Local time And format it into 2019-11-27 20:00:00 print (datetime.date.today ()) # print (datetime.date (2019, 11, 27)) # print (calendar.month (2019, 11)) # generate calendar page print (calendar.isleap (2020)) # determine whether the incoming year is a leap year print (calendar.month (2019, 11, wag3, Lhasa 2)) # modify the row spacing of the calendar Here are 3 characters and 2 characters print ('\ ntime and datetime')
Take a brief look at the output:
6. Json module-processing json:
Def test_json (self): obj = {'averse: 1,' breadth: 2} objStr = json.dumps (obj) # JavaScript JSON.stringify-serialize the JSON.parse in print (objStr) print (json.loads (objStr)) # JavaScript-- parsing "what are the modules of Python"? that's it. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.