mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 15:00:18 +00:00
more fiddling
This commit is contained in:
+1
-1
@@ -79,7 +79,7 @@ body{counter-reset:h1 3}
|
||||
<samp>c:\Users\pilgrim\diveintopython3\examples\humansize.py</samp></pre>
|
||||
<ol>
|
||||
<li>The <code>os.path.join()</code> function constructs a pathname out of one or more partial pathnames. In this case, it simply concatenates strings.
|
||||
<li>In this slightly less trivial case, <code>join</code> 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 <code>os.path.join()</code> and let Python do the right thing.
|
||||
<li>In this slightly less trivial case, calling the <code>os.path.join()</code> function 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 <code>os.path.join()</code> and let Python do the right thing.
|
||||
<li>The <code>os.path.expanduser()</code> function will expand a pathname that uses <code>~</code> 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 <code>os.path.join()</code> function doesn’t mind.
|
||||
<li>Combining these techniques, you can easily construct pathnames for directories and files in the user’s home directory. The <code>os.path.join()</code> function can take any number of arguments. I was overjoyed when I discovered this, since <code>addSlashIfNecessary()</code> is one of the stupid little functions I always need to write when building up my toolbox in a new language. <em>Do not</em> write this stupid little function in Python; smart people have already taken care of it for you.
|
||||
</ol>
|
||||
|
||||
Reference in New Issue
Block a user