Import OS; --Python comes with
Print (OS.GETCWD ())--Get the current working directory
Os.chdir ('/users/longlong/documents ')--Convert to/users/longlong/documents directory
Os.path.join (Parm1, Parm2,...)-Constructs a path name from one or more path fragments.
Os.path.expanduser ()--used to extend the path containing the ~ symbol to the full path
>>> pathname = ‘/Users/pilgrim/diveintopython3/examples/humansize.py‘
>>> os.path.split(pathname)--(‘/Users/pilgrim/diveintopython3/examples‘, ‘humansize.py‘)罗列目录内容constructing an absolute path
>>> Import os>>> Print (OS.GETCWD ())/users/longlong/documents>>> Os.chdir ("./python/") >>> os.getcwd () '/users/longlong/documents/python ' >>> print (Os.path.realpath (' whileloop.py ')
List parsing
>>> [Os.path.realpath (f) for F in Glob.glob ("*.py") ['/users/longlong/documents/python/indices_over_two_ objects.py ', '/users/longlong/documents/python/loops_over_indices.py ', '/users/longlong/documents/python/while_ Loops.py ']
>>> [F for F in Glob.glob ("*.py") if Os.stat (f). st_size > 700][' indices_over_two_objects.py ', ' While_ Loops.py ']
Dictionary parsing
>>> metadat_dict = {F:os.stat (f) for F in Glob.glob (' *.py ')}>>> type (metadat_dict) <class ' dict ' >>>> list (Metadat_dict.keys ()) [' indices_over_two_objects.py ', ' while_loops.py ', ' Loops_over_ indices.py ']>>> metadat_dict[' indices_over_two_objects.py '].st_size871
>>> list (Metadat_dict.keys ()) [' indices_over_two_objects.py ', ' while_loops.py ', ' loops_over_indices.py '] >>> a_dict = {' A ': 1, ' B ': 2, ' C ':3}>>> {Value:key for key,value in A_dict.items ()}{1: ' A ', 2: ' B ', 3: ' C '}
Set resolution
>>> A_set = set (range) >>> a_set{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}>>> {x**2 for x in a_set}{0, 1 , 4, 0, 9, +, $, Bayi, 25}>>> {x for x in A_set if x%2 = =}{0, 8, 2, 4, 6}>>> {2**x for x in rang E (10)}{32, 1, 2, 64, 4, 128, 256, 512, 8, 16}
Python Learning Note 2-parsing data