python common standard library

Keywords: Anaconda IPython Python

system interface

sys module can view and add environment variables

import sys
sys.path
>>['E:\\360MoveData\\Users\\Administrator\\Desktop\\PyTorch',
 'E:\\anzhuang\\anaconda\\python37.zip',
 'E:\\anzhuang\\anaconda\\DLLs',
 'E:\\anzhuang\\anaconda\\lib',
 'E:\\anzhuang\\anaconda',
 '',
 'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python37\\site-packages',
 'E:\\anzhuang\\anaconda\\lib\\site-packages',
 'E:\\anzhuang\\anaconda\\lib\\site-packages\\object_detection-0.1-py3.6.egg',
 'E:\\anzhuang\\anaconda\\lib\\site-packages\\slim-0.1-py3.5.egg',
 'E:\\anzhuang\\anaconda\\lib\\site-packages\\win32',
 'E:\\anzhuang\\anaconda\\lib\\site-packages\\win32\\lib',
 'E:\\anzhuang\\anaconda\\lib\\site-packages\\Pythonwin',
 'E:\\anzhuang\\anaconda\\lib\\site-packages\\IPython\\extensions',
 'C:\\Users\\Administrator\\.ipython']

sys.path.append('E:\\')
sys.path
>>['E:\\360MoveData\\Users\\Administrator\\Desktop\\PyTorch',
 'E:\\anzhuang\\anaconda\\python37.zip',
 'E:\\anzhuang\\anaconda\\DLLs',
 'E:\\anzhuang\\anaconda\\lib',
 'E:\\anzhuang\\anaconda',
 '',
 'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python37\\site-packages',
 'E:\\anzhuang\\anaconda\\lib\\site-packages',
 'E:\\anzhuang\\anaconda\\lib\\site-packages\\object_detection-0.1-py3.6.egg',
 'E:\\anzhuang\\anaconda\\lib\\site-packages\\slim-0.1-py3.5.egg',
 'E:\\anzhuang\\anaconda\\lib\\site-packages\\win32',
 'E:\\anzhuang\\anaconda\\lib\\site-packages\\win32\\lib',
 'E:\\anzhuang\\anaconda\\lib\\site-packages\\Pythonwin',
 'E:\\anzhuang\\anaconda\\lib\\site-packages\\IPython\\extensions',
 'C:\\Users\\Administrator\\.ipython',
 'E:\\']

Operating system interface

The os module provides many functions associated with the operating system.

os.getcwd()   # Return to the current working directory
>>'E:\\360MoveData\\Users\\Administrator\\Desktop\\PyTorch'

File wildcard

glob module provides a function to generate file list from directory wildcard search:

import glob
glob.glob('*.py')
>>['fibo.py', 'using_name.py']

Performance metrics

Some users are interested in understanding the performance differences between different approaches to the same problem. Python provides a measurement tool that provides direct answers to these questions.

from timeit import timeit
timeit('a = 102/2')
>>0.0231952319991251

0.0231952319991251
>>0.015161548002652125

Date and time

The datetime module provides both simple and complex methods for date and time processing.

from datetime import date
now = date.today()
now
>>datetime.date(2019, 4, 11)

birthday = date(1994,10,3)
age = now-birthday
age.days
>>8956

 

Posted by toshog on Fri, 29 Nov 2019 13:34:09 -0800