diff --git a/comprehensions.html b/comprehensions.html index 2d3b11e..785d9e7 100644 --- a/comprehensions.html +++ b/comprehensions.html @@ -180,13 +180,13 @@ body{counter-reset:h1 3}
In the previous example, the glob.glob() function returned a list of relative pathnames. The first example had pathnames like 'examples\feed.xml', and the second example had even shorter relative pathnames like 'romantest1.py'. As long as you stay in the same current working directory, these relative pathnames will work for opening files or getting file metadata. But if you want to construct an absolute pathname — i.e. one that includes all the directory names back to the root directory or drive letter — then you’ll need the os.path.abspath() function.
+
In the previous example, the glob.glob() function returned a list of relative pathnames. The first example had pathnames like 'examples\feed.xml', and the second example had even shorter relative pathnames like 'romantest1.py'. As long as you stay in the same current working directory, these relative pathnames will work for opening files or getting file metadata. But if you want to construct an absolute pathname — i.e. one that includes all the directory names back to the root directory or drive letter — then you’ll need the os.path.realpath() function.
>>> import os
>>> print(os.getcwd())
c:\Users\pilgrim\diveintopython3\examples
->>> print(os.path.abspath('feed.xml'))
+>>> print(os.path.realpath('feed.xml'))
c:\Users\pilgrim\diveintopython3\examples\feed.xml
>>> import os, glob
->>> glob.glob('*.xml') ①
+>>> glob.glob('*.xml') ①
['feed-broken.xml', 'feed-ns0.xml', 'feed.xml']
->>> [os.path.abspath(f) for f in glob.glob('*.xml')] ②
+>>> [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']
@@ -244,7 +244,7 @@ body{counter-reset:h1 3}
>>> import os, glob
->>> [(os.stat(f).st_size, os.path.abspath(f)) for f in glob.glob('*.xml')] ①
+>>> [(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')]
@@ -254,7 +254,7 @@ body{counter-reset:h1 3}
('3.3 KiB', 'feed-ns0.xml'),
('3.0 KiB', 'feed.xml')]
-- This list comprehension finds all the
.xml files in the current working directory, gets the size of each file (by calling the os.stat() function), and constructs a tuple of the file size and the absolute path of each file (by calling the os.path.abspath() function).
+ - This list comprehension finds all the
.xml files in the current working directory, gets the size of each file (by calling the os.stat() function), and constructs a tuple of the file size and the absolute path of each file (by calling the os.path.realpath() function).
- This comprehension builds on the previous one to call the
approximate_size() function with the file size of each .xml file.