Download presentation
Presentation is loading. Please wait.
Published byMathew Fanning Modified over 10 years ago
1
Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Python
2
Directory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' planets.txt data vladusers We want to build a path
3
PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = base + '/' + user + '/' + datadir >>> print path /users/vlad/data planets.txt data vladusers Use string addition
4
PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = base + '/' + user + '/' + datadir >>> print path /users/vlad/data Code assumes Linux/UNIX paths
5
PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = base + '/' + user + '/' + datadir >>> print path /users/vlad/data >>> from os.path import join
6
PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = base + '/' + user + '/' + datadir >>> print path /users/vlad/data >>> from os.path import join >>> path = join(base, user, datadir)
7
PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = base + '/' + user + '/' + datadir >>> print path /users/vlad/data >>> from os.path import join >>> path = join(base, user, datadir) >>> print path /users/vlad/data
8
PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = base + '/' + user + '/' + datadir >>> print path /users/vlad/data >>> from os.path import join >>> path = join(base, user, datadir) >>> print path /users/vlad/data join picks the file separator based on the current operating system
9
PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = join(base, user, datadir) >>> print path /users\\vlad\\data Running under Windows
10
PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = join(base, user, datadir) >>> print path /users\\vlad\\data Running under Windows join chooses the Windows separator The double \ is only because we’re printing them
11
PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = join(base, user, datadir) >>> print path /users\\vlad\\data Running under Windows join chooses the Windows separator The double \ is only because we’re printing them But it starts with a Linux/UNIX file separator How do we convert that?
12
PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = join(base, user, datadir) >>> print path /users\\vlad\\data >>> from os.path import normpath
13
PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = join(base, user, datadir) >>> print path /users\\vlad\\data >>> from os.path import normpath >>> nupath = normpath(path) >>> print nupath \\users\\vlad\\data
14
PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = join(base, user, datadir) >>> print path /users\\vlad\\data >>> from os.path import normpath >>> nupath = normpath(path) >>> print nupath \\users\\vlad\\data >>> normpath('/some/other/path') \\some\\other\\path normpath converts path separators
15
PythonDirectory and File Paths >>> path = '/users/vlad//.///data/../..//vlad/./data/..' normpath not only converts path separators
16
PythonDirectory and File Paths >>> path = '/users/vlad//.///data/../..//vlad/./data/..' >>> cleanpath = normpath(path) >>> print cleanpath /users/vlad normpath not only converts path separators
17
PythonDirectory and File Paths >>> path = '/users/vlad//.///data/../..//vlad/./data/..' >>> cleanpath = normpath(path) >>> print cleanpath /users/vlad normpath not only converts path separators, it removes duplicated path separators
18
PythonDirectory and File Paths >>> path = '/users/vlad//.///data/../..//vlad/./data/..' >>> cleanpath = normpath(path) >>> print cleanpath /users/vlad normpath not only converts path separators, it removes duplicated path separators and “.” current directory short-hand
19
PythonDirectory and File Paths >>> path = '/users/vlad//.///data/../..//vlad/./data/..' >>> cleanpath = normpath(path) >>> print cleanpath /users/vlad normpath not only converts path separators, it Removes duplicated path separators Removes “.” current directory short-hand Tries to resolve “..” parent directory short-hand
20
PythonDirectory and File Paths >>> from os.path import dirname, basename
21
PythonDirectory and File Paths >>> from os.path import dirname, basename >>> path = '/users/vlad/data/planets.txt' planets.txt data vladusers
22
PythonDirectory and File Paths >>> from os.path import dirname, basename >>> path = '/users/vlad/data/planets.txt' >>> dirname(path) /users/vlad/data planets.txt data vladusers
23
PythonDirectory and File Paths >>> from os.path import dirname, basename >>> path = '/users/vlad/data/planets.txt' >>> dirname(path) /users/vlad/data >>> basename(path) planets.txt data vladusers
24
PythonDirectory and File Paths >>> from os.path import dirname, basename >>> path = '/users/vlad/data/planets.txt' >>> dirname(path) /users/vlad/data >>> basename(path) planets.txt >>> from os.path import split >>> (head, tail) = split(path) planets.txt data vladusers
25
PythonDirectory and File Paths >>> from os.path import dirname, basename >>> path = '/users/vlad/data/planets.txt' >>> dirname(path) /users/vlad/data >>> basename(path) planets.txt >>> from os.path import split >>> (head, tail) = split(path) >>> print head /users/vlad/data planets.txt data vladusers
26
PythonDirectory and File Paths >>> from os.path import dirname, basename >>> path = '/users/vlad/data/planets.txt' >>> dirname(path) /users/vlad/data >>> basename(path) planets.txt >>> from os.path import split >>> (head, tail) = split(path) >>> print head /users/vlad/data >>> print tail planets.txt data vladusers
27
PythonDirectory and File Paths >>> from os.path import splitext >>> path = '/users/vlad/data/planets.txt'
28
PythonDirectory and File Paths >>> from os.path import splitext >>> path = '/users/vlad/data/planets.txt' >>> (root, ext) = splitext(path)
29
PythonDirectory and File Paths >>> from os.path import splitext >>> path = '/users/vlad/data/planets.txt' >>> (root, ext) = splitext(path) >>> print root /users/vlad/data/planets
30
PythonDirectory and File Paths >>> from os.path import splitext >>> path = '/users/vlad/data/planets.txt' >>> (root, ext) = splitext(path) >>> print root /users/vlad/data/planets >>> print ext.txt
31
PythonDirectory and File Paths >>> from os.path import splitdrive >>> path = 'C:\\users\\vlad\\data\\planets.txt' >>> (drive, tail) = splitdrive(path)
32
PythonDirectory and File Paths >>> from os.path import splitdrive >>> path = 'C:\\users\\vlad\\data\\planets.txt' >>> (drive, tail) = splitdrive(path) >>> print drive C:
33
PythonDirectory and File Paths >>> from os.path import splitdrive >>> path = 'C:\\users\\vlad\\data\\planets.txt' >>> (drive, tail) = splitdrive(path) >>> print drive C: >>> print tail \\users\\vlad\\data\\planets.txt
34
PythonDirectory and File Paths >>> path = 'data/planets.txt' planets.txt data
35
PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False planets.txt data isabs checks is the path is absolute
36
PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False isabs checks is the path is absolute Linux/UNIX: does it start with / ? Windows: does it start with \ after the drive’s been removed? planets.txt data
37
PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) planets.txt data vladusers abspath makes relative paths absolute
38
PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) >>> print abspath /users/vlad/data/planets.txt planets.txt data vladusers abspath makes relative paths absolute
39
PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) >>> print abspath /users/vlad/data/planets.txt planets.txt data vladusers abspath makes relative paths absolute It uses getcwd()
40
PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) >>> print abspath /users/vlad/data/planets.txt >>> isabs(nupath) True planets.txt data vladusers
41
PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) >>> print abspath /users/vlad/data/planets.txt >>> isabs(nupath) True >>> abspath('data/../..') data..
42
PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) >>> print abspath /users/vlad/data/planets.txt >>> isabs(nupath) True >>> abspath('data/../..') /users data vladusers..
43
PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) >>> print abspath /users/vlad/data/planets.txt >>> isabs(nupath) True >>> abspath('data/../..') /users data vladusers abspath also normalizes the path
44
PythonDirectory and File Paths Beware! None of these operations check whether the directories or files exist
45
PythonDirectory and File Paths Beware! None of these operations check whether the directories or files exist Remember os.path exists function
46
PythonDirectory and File Paths os.path Common pathname manipulations join Join relative paths together using operating system- specific separators normpath Clean up a path and convert separators to be consistent with the current operating system dirname Get the path up to the final directory/file in the path basename Get the final directory/file in the path split Split a path into directory and file name splitext Split a path to get a file extension splitdrive Split a path to get a drive name isabs Is a path relative or absolute? abspathConvert a path to an absolute path, using getcwd()
47
May 2011 created by Mike Jackson and Greg Wilson Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.