.


:




:

































 

 

 

 





glob.glob() . 'examples\feed.xml', , , 'romantest1.py'. , . , , os.path.realpath().

>>> import os
>>> print(os.getcwd())
c:\Users\pilgrim\diveintopython3\examples
>>> print(os.path.realpath('feed.xml'))

c:\Users\pilgrim\diveintopython3\examples\feed.xml

Python.

, .

>>> a_list = [1, 9, 8, 4] >>> [elem * 2 for elem in a_list] [2, 18, 16, 8] [1]
>>> a_list [1, 9, 8, 4] [2]
>>> a_list = [elem * 2 for elem in a_list] >>> a_list [2, 18, 16, 8] [3]
  1. ↑ , , . a_list . Python a_list, elem. elem * 2 .
  2. ↑ , .
  3. ↑ . Python , , .

Python, os, .

>>> import os, glob >>> glob.glob('*.xml') ['feed-broken.xml', 'feed-ns0.xml', 'feed.xml'] [1]
>>> [os.path.realpath(f) for f in glob.glob('*.xml')] ['c:\\Users\\pilgrim\\diveintopython3\\examples\\feed-broken.xml', 'c:\\Users\\pilgrim\\diveintopython3\\examples\\feed-ns0.xml', 'c:\\Users\\pilgrim\\diveintopython3\\examples\\feed.xml'] [2]
  1. ↑ .xml- .
  2. ↑ .xml- .

, .

>>> import os, glob
>>> [f for f in glob.glob('*.py') if os.stat(f).st_size > 6000] ['pluraltest6.py', 'romantest10.py', 'romantest6.py', 'romantest7.py', 'romantest8.py', 'romantest9.py'] [1]
  1. ↑ , if . , if, . , . .py- , if , 6000 . , .

: , ( ). .

>>> import os, glob
>>> [(os.stat(f).st_size, os.path.realpath(f)) for f in glob.glob('*.xml')] [(3074, 'c:\\Users\\pilgrim\\diveintopython3\\examples\\feed-broken.xml'), (3386, 'c:\\Users\\pilgrim\\diveintopython3\\examples\\feed-ns0.xml'), (3070, 'c:\\Users\\pilgrim\\diveintopython3\\examples\\feed.xml')] >>> import humansize [1]
>>> [(humansize.approximate_size(os.stat(f).st_size), f) for f in glob.glob('*.xml')] [('3.0 KiB', 'feed-broken.xml'), ('3.3 KiB', 'feed-ns0.xml'), ('3.0 KiB', 'feed.xml')] [2]
  1. ↑ .xml- , ( os.stat()), ( os.path.realpath()).
  2. ↑ , , approximate_size(), .xml-.

, .

>>> import os, glob
>>> metadata = [(f, os.stat(f)) for f in glob.glob('*test*.py')] [1]
>>> metadata[0] ('alphameticstest.py', nt.stat_result(st_mode=33206, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=2509, st_atime=1247520344, st_mtime=1247520344, st_ctime=1247520344)) [2]
>>> metadata_dict = {f:os.stat(f) for f in glob.glob('*test*.py')} [3]
>>> type(metadata_dict) <class 'dict'> [4]
>>> list(metadata_dict.keys()) ['romantest8.py', 'pluraltest1.py', 'pluraltest2.py', 'pluraltest5.py', 'pluraltest6.py', 'romantest7.py', 'romantest10.py', 'romantest4.py', 'romantest9.py', 'pluraltest3.py', 'romantest1.py', 'romantest2.py', 'romantest3.py', 'romantest5.py', 'romantest6.py', 'alphameticstest.py', 'pluraltest4.py'] [5]
>>> metadata_dict['alphameticstest.py'].st_size 2509 [6]
  1. ↑ , . .py, , ( os.stat()).
  2. ↑ .
  3. ↑ . , . -, , . -, , . ( f) ; ( os.stat(f)) .
  4. ↑ .
  5. ↑ , glob.glob('*test*.py').
  6. ↑ , , os.stat(). , . (st_size) . alphameticstest.py 2509 .

, , if, -, .

>>> import os, glob, humansize
>>> metadata_dict = {f:os.stat(f) for f in glob.glob('*')} [1]
>>> humansize_dict = {os.path.splitext(f)[0]:humansize.approximate_size(meta.st_size) \... for f, meta in metadata_dict.items() if meta.st_size > 6000} [2]
>>> list(humansize_dict.keys()) ['romantest9', 'romantest8', 'romantest7', 'romantest6', 'romantest10', 'pluraltest6'] [3]
>>> humansize_dict['romantest9'] '6.5 KiB' [4]
  1. ↑ (glob.glob('*')), (os.stat(f)) , , .
  2. ↑ . 6000 (if meta.st_size > 6000). , (os.path.splitext(f)[0]), (humansize.approximate_size(meta.st_size)).
  3. ↑ , , , .
  4. ↑ , approximate_size().




:


: 2016-11-18; !; : 368 |


:

:

, .
==> ...

1533 - | 1325 -


© 2015-2024 lektsii.org - -

: 0.008 .