From eab3da2f9adbd8c7d847c812414f5d60e1ef50c6 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Sun, 16 Aug 2009 10:36:49 -0400 Subject: [PATCH] use realpath instead of abspath --- comprehensions.html | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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}

Constructing Absolute Pathnames

-

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

List Comprehensions

@@ -212,9 +212,9 @@ body{counter-reset:h1 3}
 >>> 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')]
    -
  1. 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). +
  2. 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).
  3. This comprehension builds on the previous one to call the approximate_size() function with the file size of each .xml file.