diff --git a/comprehensions.html b/comprehensions.html index 57e0215..c10632c 100644 --- a/comprehensions.html +++ b/comprehensions.html @@ -79,7 +79,7 @@ body{counter-reset:h1 3} c:\Users\pilgrim\diveintopython3\examples\humansize.py
  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 slash to the pathname before joining it to the filename. It’s a backslash instead of a forward slash, because I constructed this example on Windows. If you replicate this example on Linux or Mac OS X, you’ll see a forward slash instead. Python can access the file regardless of what kind of slashes you use in the pathname. +
  3. In this slightly less trivial case, join will add an extra slash to the pathname before joining it to the filename. It’s a backslash instead of a forward slash, because I constructed this example on Windows. If you replicate this example on Linux or Mac OS X, you’ll see a forward slash instead. Don’t fuss with slashes; always use os.path.join() and let Python do the right thing.
  4. 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.
  5. 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. 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.