Python Basics Module

Source: Internet
Author: User

One. OS Module

1.Os.path.abspath (path), returns the absolute path normalized by path

Import Osprint (Os.path.abspath (r "D:\a\ new text document. txt")) "" "D:\a\ new text document. txt" ""

2.os.path.split (path), split path into directory and file name two tuples returned

Print (Os.path.split (r "D:\a\ new text document. txt") "" "" "(' d:\\a ', ' New text document '. txt ')" ""

3.os.path. dirname (path), which returns the directory of path. is actually the first element of Os.path.split (path)

Print (Os.path.dirname (r "D:\a\ new text document. txt") "" "" "D:\a" ""

4.os.path.basename(), returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path)

Print (Os.path.basename (r "D:\a\ new text document. txt"))
"" New text document. txt "" "

5.os.path.exists (), returns True if path exists, false if path does not exist

Print (Os.path.exists (r "D:\a\ new text document. txt")) "" "True" ""

6.os.path.isabs (), if path is an absolute path, returns True

Print (Os.path.isabs (r "D:\a\ new text document. txt")) "" "True" ""

7.os.path.isfile (), returns True if path is an existing file. otherwise returns false

Print (Os.path.isfile (r "D:\a\ new text document. txt")) "" "True" ""

8.os.path.isdir () returns True if path is a directory that exists. otherwise returns false

Print (Os.path.isdir (r "d:\a")) "" "" True "" "

9.os.path.join () Returns a combination of multiple paths, the parameters before the first absolute path are ignored

Print (Os.path.join (' A ', R ' d:\b ', ' new text file. txt ') "" "" "D:\b\ new text file. txt" ""

10.os.path.getatime (), returns the last access time of the file or directory to which path is pointing

Print (Os.path.getatime (r "D:\a\ new text document. txt") "" "" "1496674596.1832142" ""

11.os.path.getmtime (), returns the last modified time of the file or directory to which path is pointing

Print (Os.path.getmtime (r "D:\a\ new text document. txt") "" "" "1496674596.1832142" ""
Two. Random module

1. Remove the random IP address

Import randomproxy_ip=[    ' 192.1.1.1 ',    ' 192.1.1.2 ',    ' 192.1.1.3 ',    ' 192.1.1.4 ',]print (random.choice (PROXY_IP))

2. Verification Code Instance

Import Randomdef V_code (n=5):    res= ' for    i in range (n):        num=random.randint (0,9)        S=CHR ( Random.randint (65,90))        add=random.choice ([num,s])        res+=str (add)    return Resprint (V_code (6))
Three. SYS module

Progress bar, invalid under Pycharm, executed at command line

Import sys,timefor i in range:    sys.stdout.write ('%s\r '% (' # ' *i))    Sys.stdout.flush ()    Time.sleep (0.1)
Four. Time module

1. Timestamp, seconds after January 1, 1970

Import Timeprint (Time.time ()) "" "1496677931.1134853" ""

2. Structured time

Print (Time.localtime ()) print (Time.localtime (). Tm_year) Print (Time.gmtime ()) "" "Time.struct_time (tm_year=2017, tm_ Mon=6, Tm_mday=5, tm_hour=23, tm_min=54, tm_sec=45, tm_wday=0, tm_yday=156, tm_isdst=0) 2017time.struct_time (tm_year= Tm_mon=6, Tm_mday=5, tm_hour=15, tm_min=54, tm_sec=45, tm_wday=0, tm_yday=156, tm_isdst=0) "" "

3. Structured string, default to the current time

Print (Time.strftime ('%y-%m-%d%h:%m:%s ')) print (Time.strftime ('%y-%m-%d%x ')) "" "2017-06-05 23:55:482017-06-05 23:55:48 "" "

Python Basics Module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.