Python3 tutorial day5

Standard Library

 

OS

import os

os.getcwd()

os.chdir("/home/andy")

os.system("mkdir today")

>>> import shutil
>>> dir(shutil)
['Error', 'ExecError', 'ReadError', 'RegistryError', 'SpecialFileError', 'WindowsError', '_ARCHIVE_FORMATS', '_BZ2_SUPPORTED', '_UNPACK_FORMATS', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__name__', '__package__', '_basename', '_call_external_zip', '_check_unpack_options', '_destinsrc', '_ensure_directory', '_find_unpack_format', '_get_gid', '_get_uid', '_make_tarball', '_make_zipfile', '_samefile', '_unpack_tarfile', '_unpack_zipfile', 'abspath', 'bz2', 'collections', 'copy', 'copy2', 'copyfile', 'copyfileobj', 'copymode', 'copystat', 'copytree', 'errno', 'fnmatch', 'get_archive_formats', 'get_unpack_formats', 'getgrnam', 'getpwnam', 'ignore_patterns', 'make_archive', 'move', 'os', 'register_archive_format', 'register_unpack_format', 'rmtree', 'stat', 'sys', 'tarfile', 'unpack_archive', 'unregister_archive_format', 'unregister_unpack_format']
>>>
>>>
>>>
>>> shutil.copyfile('f1','f2')
>>> os.system('ls')
binary_write commandArgv.py f1 f2 fibo.py __init__.py __pycache__ workfile
0
>>> shutil.move('f2','file2')
>>> os.system('ls')
binary_write commandArgv.py f1 fibo.py file2 __init__.py __pycache__ workfile

 

File wildcards

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

 

command line arguments

import sys

print(sys.argv)

 

input/output

sys.stdin/stdout/stderr

 

String pattern matching

 

re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
>>> re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')
'cat in the hat'

 

Mathematics

>>> import math
>>> math.cos(math.pi/4)
0.7071067811865476
>>> math.log(1024,2)
10.0
>>> import random
>>> random.choice(['apple','pear','banana'])
'banana'
>>> random.sample(range(100),10)
[0, 98, 68, 96, 76, 46, 85, 43, 27, 80]
>>> random.random()
0.8395295007414879
>>> random.randrange(6)
1

 

Internet Access

from urllib.request import urlopen

Dates and times

from datetime import date

 

Data compression

zlib

 

Performance Measurement

modules timeit/profile/pstats

 

output Formating

reprlib/pprint/textwrap/locale

from string import Template

 

Multi-threading

threading

 

posted on 2014-10-24 18:07  ukouryou  阅读(120)  评论(0编辑  收藏  举报

导航