Python basis of datetime, sys module

1.datetime module

1) datetime.datetime.now (), returns the respective current time, date type.

  datetime.datetime.now (), returns the current date.

. 1  Import datetime
 2 dt = datetime.datetime.now ()
 . 3  Print (dt)
 . 4  Results:
 . 5  2019 - 12 is - 16  . 11 : 23 is : 58.066730

  datetime.datetime.now (). year, returns the current year, month, day.

1 import datetime
2 dt = datetime.datetime.now()
3 print(dt.year)
4 print(dt.month)
5 print(dt.day)
6 结果:
7 2019
8 12
9 16

  datetime.datetime.now (). replace (), the replacement time, replace the specified date.

1 import datetime
2 dt = datetime.datetime.now()
3 print(dt)
4 print(dt.replace(year=2020,month=1,day=1))
5 结果:
6 2019-12-16 11:54:29.262599
7 2020-01-01 11:54:29.262599

  datetime.datetime.now (). time (), returns the current time, time tuple (struct_time), timestamp.

1 import datetime
2 dt = datetime.datetime.now()
3 print(dt.time())
4 print(dt.timetuple())
5 print(dt.timestamp())
6 结果:
7 11:27:55.867669
8 time.struct_time(tm_year=2019, tm_mon=12, tm_mday=16, tm_hour=11, tm_min=27, tm_sec=55, tm_wday=0, tm_yday=350, tm_isdst=-1)
9 1576466875.867669

2)datetime.date.fromtimestamp()

  The timestamp into a datetime date type.

1 import datetime
2 import time
3 dt = time.time()
4 print(datetime.date.fromtimestamp(dt))
5 结果:
6 2019-12-16

3) datetime.timedelta ()

  datetime.datetime.now () + datetime.timedelta (n), n represents the applied current datetime days time.

1  import datetime
 2 dt = datetime.datetime.now ()
 3  print (dt)
 4 print (dt + datetime.timedelta ( 2 ))
 5  结果:
 6  2019 - 12 - 16  11 : 47 : 45.999598 
7  2019 - 12 - 18  11 : 47 : 45.999598

  datetime.timedelta (hours = n), represents the increase in the current time n datetime hours.

1 import datetime
2 dt = datetime.datetime.now()
3 print(dt)
4 print(dt + datetime.timedelta(hours=2))
5 结果:
6 2019-12-16 11:50:27.621787
7 2019-12-16 13:50:27.621787

2.sys module

  sys module provides a series of python runtime environment variables and functions.

1)sys.argv

  The list of parameters of command line parameters is currently being executed.

. 1  Import SYS
 2  Print (the sys.argv)
 . 3  Results:
 . 4 [ ' F.: / Python / week2 / module .py ' ]

2)sys.platform

  Gets the current execution environment platform, win32 represent windows 32bit operating system, linux2 expressed linux platform.

. 1  Import SYS
 2  Print (sys.platform)
 . 3  Results:
 . 4 Win32

3)sys.path

  path is a directory listing, look for third-party extensions from python module; sometimes in order to allow python to find their own definition module, you need to modify the list sys.path, () the name of the custom plug-in module that is used sys.path.insert can.

1 import sys
2 print(sys.path)
3 结果:
4 ['F:\\python\\week2', 'F:\\python', 'F:\\python\\python3.5.2\\python35.zip', 'F:\\python\\python3.5.2\\DLLs', 'F:\\python\\python3.5.2\\lib', 'F:\\python\\python3.5.2', 'F:\\python\\python3.5.2\\lib\\site-packages']

4)sys.builtin_module_names

  Returns a list that contains the name of the built-in module.

1 import sys
2 print(sys.builtin_module_names)
3 结果:
4 ('_ast', '_bisect', '_codecs', '_codecs_cn', '_codecs_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_collections', '_csv', '_datetime', '_functools', '_heapq', '_imp', '_io', '_json', '_locale', '_lsprof', '_md5', '_multibytecodec', '_opcode', '_operator', '_pickle', '_random', '_sha1', '_sha256', '_sha512', '_signal', '_sre', '_stat', '_string', '_struct', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', '_winapi', 'array', 'atexit', 'audioop', 'binascii', 'builtins', 'cmath', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'math', 'mmap', 'msvcrt', 'nt', 'parser', 'sys', 'time', 'winreg', 'xxsubtype', 'zipimport', 'zlib')
View Code

5)sys.exit()

  Call sys.exit () you can drop out program.

Guess you like

Origin www.cnblogs.com/foxshu/p/12048420.html
Recommended