From 7db9ef3ce41dfbd12b0636c3f61f65020cca8899 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Sun, 16 Aug 2009 10:46:06 -0400 Subject: [PATCH] clarify some stuff about trailing slashes --- comprehensions.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/comprehensions.html b/comprehensions.html index 4a382d2..702948b 100644 --- a/comprehensions.html +++ b/comprehensions.html @@ -78,8 +78,8 @@ body{counter-reset:h1 3}
  1. The os.path.join() function constructs a pathname out of one or more partial pathnames. In this case, it simply concatenates strings.
  2. In this slightly less trivial case, join will add an extra backslash to the pathname before joining it to the filename. I was overjoyed when I discovered this, since addSlashIfNecessary() is one of the stupid little functions I always need to write when building up my toolbox in a new language. Do not write this stupid little function in Python; smart people have already taken care of it for you. -
  3. The os.path.expanduser() function will expand a pathname that uses ~ to represent the current user’s home directory. This works on any platform where users have a home directory, including Linux, Mac OS X, and Windows. -
  4. Combining these techniques, you can easily construct pathnames for directories and files under the user’s home directory. +
  5. The os.path.expanduser() function will expand a pathname that uses ~ to represent the current user’s home directory. This works on any platform where users have a home directory, including Linux, Mac OS X, and Windows. The returned path does not have a trailing slash, but the os.path.join() function doesn’t mind. +
  6. Combining these techniques, you can easily construct pathnames for directories and files in the user’s home directory. The os.path.join() function can take any number of arguments.

os.path also contains functions to split full pathnames, directory names, and filenames into their constituent parts. @@ -99,8 +99,8 @@ body{counter-reset:h1 3} >>> extension '.py'

    -
  1. The split function splits a full pathname and returns a tuple containing the path and filename. Remember when I said you could use multi-variable assignment to return multiple values from a function? The os.path.split() function does exactly that. -
  2. You assign the return value of the split function into a tuple of two variables. Each variable receives the value of the corresponding element of the returned tuple. +
  3. The split function splits a full pathname and returns a tuple containing the path and filename. +
  4. Remember when I said you could use multi-variable assignment to return multiple values from a function? The os.path.split() function does exactly that. You assign the return value of the split function into a tuple of two variables. Each variable receives the value of the corresponding element of the returned tuple.
  5. The first variable, dirname, receives the value of the first element of the tuple returned from the os.path.split() function, the file path.
  6. The second variable, filename, receives the value of the second element of the tuple returned from the os.path.split() function, the filename.
  7. os.path also contains the os.path.splitext() function, which splits a filename and returns a tuple containing the filename and the file extension. You use the same technique to assign each of them to separate variables.