47
Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 20 This work is licensed under the Creative Commons Attribution Lice See http://software-carpentry.org/license.html for more informati Python

Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Embed Size (px)

Citation preview

Page 1: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

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

Page 2: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> base = '/users'

>>> user = 'vlad'

>>> datadir = 'data'

planets.txt

data

vlad

usersWe want to build a path

Page 3: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> base = '/users'

>>> user = 'vlad'

>>> datadir = 'data'

>>> path = base + '/' + user + '/' + datadir

>>> print path

/users/vlad/data

planets.txt

data

vlad

usersUse string addition

Page 4: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> base = '/users'

>>> user = 'vlad'

>>> datadir = 'data'

>>> path = base + '/' + user + '/' + datadir

>>> print path

/users/vlad/data Code assumes Linux/UNIX paths

Page 5: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> base = '/users'

>>> user = 'vlad'

>>> datadir = 'data'

>>> path = base + '/' + user + '/' + datadir

>>> print path

/users/vlad/data

>>> from os.path import join

Page 6: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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)

Page 7: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

Page 8: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

Page 9: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> base = '/users'

>>> user = 'vlad'

>>> datadir = 'data'

>>> path = join(base, user, datadir)

>>> print path

/users\\vlad\\data

Running under Windows

Page 10: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

Page 11: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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?

Page 12: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> base = '/users'

>>> user = 'vlad'

>>> datadir = 'data'

>>> path = join(base, user, datadir)

>>> print path

/users\\vlad\\data

>>> from os.path import normpath

Page 13: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

Page 14: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

Page 15: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> path = '/users/vlad//.///data/../..//vlad/./data/..'

normpath not only converts path separators

Page 16: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> path = '/users/vlad//.///data/../..//vlad/./data/..'

>>> cleanpath = normpath(path)

>>> print cleanpath

/users/vlad

normpath not only converts path separators

Page 17: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

Page 18: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

Page 19: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

Page 20: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> from os.path import dirname, basename

Page 21: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> from os.path import dirname, basename

>>> path = '/users/vlad/data/planets.txt'

planets.txt

data

vlad

users

Page 22: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> from os.path import dirname, basename

>>> path = '/users/vlad/data/planets.txt'

>>> dirname(path)

/users/vlad/data

planets.txt

data

vlad

users

Page 23: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> from os.path import dirname, basename

>>> path = '/users/vlad/data/planets.txt'

>>> dirname(path)

/users/vlad/data

>>> basename(path)

planets.txt

planets.txt

data

vlad

users

Page 24: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

vlad

users

Page 25: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

vlad

users

Page 26: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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 planets.txt

data

vlad

users

Page 27: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> from os.path import splitext

>>> path = '/users/vlad/data/planets.txt'

Page 28: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> from os.path import splitext

>>> path = '/users/vlad/data/planets.txt'

>>> (root, ext) = splitext(path)

Page 29: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> from os.path import splitext

>>> path = '/users/vlad/data/planets.txt'

>>> (root, ext) = splitext(path)

>>> print root

/users/vlad/data/planets

Page 30: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

Page 31: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> from os.path import splitdrive

>>> path = 'C:\\users\\vlad\\data\\planets.txt'

>>> (drive, tail) = splitdrive(path)

Page 32: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> from os.path import splitdrive

>>> path = 'C:\\users\\vlad\\data\\planets.txt'

>>> (drive, tail) = splitdrive(path)

>>> print drive

C:

Page 33: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

Page 34: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

>>> path = 'data/planets.txt'

planets.txt

data

Page 35: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

Page 36: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

Page 37: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

vlad

users

abspath makes relative paths absolute

Page 38: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

vlad

users

abspath makes relative paths absolute

Page 39: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

vlad

users

abspath makes relative paths absolute

It uses getcwd()

Page 40: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

vlad

users

Page 41: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

..

..

Page 42: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

vlad

users

..

..

Page 43: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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

vlad

users

abspath also normalizes the path

Page 44: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

Beware!

None of these operations check whether the

directories or files exist

Page 45: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory and File Paths

Beware!

None of these operations check whether the

directories or files exist

Remember os.path exists function

Page 46: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Directory 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?

abspath Convert a path to an absolute path, using getcwd()

Page 47: Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

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.