Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand random&os&sys module

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to understand the random&os&sys module, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Random module

Import random

Print (random.random ()) # take a floating point value between 0 and 1

Print (random.randint (0prime9)) # Random integers from 0 to 9

Print (random.randrange (1 and 10)) # range 1-9 randomly take a number that contains 1 but not 9

Print (random.choice (1, 2) 3, 4)) # choice (string or list tuple) randomly removes an element or letter

Print (random.sample ('hello',2)) # randomly takes 2 digits

Print (random.uniform (1, 10)) # specifies a floating point number between 1 and 10

Lis= [1,2,3,4,5,6]

Random.shuffle (lis) # reshuffle the lis list

Print (lis)

# randomly generate CAPTCHA instances

Checkcode=''

For i in range (6):

Current=random.randrange (0Pol 6) # 0 1 2 take one at random

If current = = I:

Tmp = chr (random.randrange (65, 991)) # 65 to 90 take one at random

Else:

Tmp=random.randint (0Pol 9) # 0-9 randomly pick one

Checkcode+=str (tmp) # tmp overlay character overlay 6 times checkcode=str (tmp) + Last str (tmp) value

Print (checkcode)

Os module

Import os

Print (os.getcwd ()) # getting the current operation directory is similar to pwd

Os.chdir (r "D:\ project\ s14") # switch to the D:\ project directory

Print (os.getcwd ())

Print (os.curdir) # represents the current directory.

Print (os.pardir) # represents the directory at the next level..

Os.makedirs (r "D:\ a\ b\ c\ d") # Recursive create directory

Os.removedirs (r "D:\ a\ b\ c\ d") # Recursively delete something in the empty folder that is not deleted

Os.mkdir (r "D:\ b") # can only create one-level directories

Os.rmdir (r "D:\ b") # can only delete one level of directory

Print (os.listdir (r "D:\ project\ S14\ day5") # lists the directories or files in the current directory or in the specified directory

Print (os.listdir (".\ day5")) # day5 at the next level of the current directory

Print (os.listdir ()) # default current directory

Os.remove ("a.txt") # Delete a.txt files in the current directory or specified directory

Os.rename ("a", "b") # rename a file or folder, default to the current directory, path can be specified

Print (os.stat ('b.txt')) # View the b.txt file information in the current directory

Import time

Tm=time.localtime (1543050404) # tuple

Print (time.strftime ("% Y-%m-%d% H:%M:%S", tm)) # formatting time

Print (os.sep) # gets the path separator of the operating system under cmd is\\

Print (os.linesep) # get the newline character win is\ r\ n linux is\ n

The print (os.pathsep) # splitter file indicates that the delimiter win for each path is; linux is:

Print (os.environ) # get environment variables

Print (os.name) # get the current operating system name

Os.system ('dir') # is used to run cmd window commands

Os.system ('ipconfig')

Print (os.path.abspath (_ _ file__)) # gets the absolute path of the current file or directory, or you can specify other files or directories under the current directory

Print (os.path.abspath ("b"))

Print (os.path.split (r "D:\ project\ S14\ b.txt") # displays the tuple split directory and file name

Print (os.path.dirname (r "D:\ project\ S14\ b.txt")) # take the directory name of the file

Print (os.path.basename (r "..\ b.txt")) # take the file name

Print (os.path.exists (rusted:\ project')) # determines whether the directory exists, and if it exists, it is true.

Print (os.path.isabs (rattd:\ project\ s14\ b.txt') # determines whether it is an absolute path. If it is true, the path may not exist.

Print (os.path.isfile ("b.txt")) # determines whether the b.txt in the current directory is an ordinary file, and if it does not exist or the directory is false

# print (os.getcwd ())

# print (os.system ('dir')

Print (os.path.isdir ("b")) # determines whether it is a directory or not

# print (os.path.join (ringing Doreau recording, r'\ a recording, r'\ baking, r'\ d.txt')) # unknown

Print (os.path.getatime (rattd:\ project\ S14\ b.txt')) # get the access time of D:\ project\ S14\ b.txt file

Tm=os.path.getatime (ringing D:\ project\ s14\ b.txt') # timestamp

Print (time.strftime ('% Y-%m-%d% HGV% M% Shop time.localtime (tm)) # timestamps are converted to tuples and the output is formatted

Print (os.path.getmtime (r "D:\ project\ S14\ b.txt") # get the modification time of D:\ project\ S14\ b.txt file

Tmm=os.path.getmtime (r "D:\ project\ S14\ b.txt")

Print (time.strftime ('% Y-%m-%d -% HGV% MRV% Shop Magazine time.localtime (tmm)

Sys module

Import sys,os

Print (sys.version) # View python version

Print (sys.argv)

# change to the current directory in terminal and execute python os_1.py 1 2 3 4 5

# you can pass in 5 parameters

The above is how to understand the random&os&sys module, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report