54
Browsing Directories 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

Browsing Directories 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: Browsing Directories Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Browsing Directories

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: Browsing Directories Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Browsing Directories

We can use Python to

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

Python Browsing Directories

We can use Python to

– Save data to files

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

Python Browsing Directories

We can use Python to

– Save data to files

– Read data from files

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

Python Browsing Directories

We can use Python to

– Save data to files

– Read data from files

But we might also want to

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

Python Browsing Directories

We can use Python to

– Save data to files

– Read data from files

But we might also want to

– See what files we have

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

Python Browsing Directories

We can use Python to

– Save data to files

– Read data from files

But we might also want to

– See what files we have

– Delete files

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

Python Browsing Directories

We can use Python to

– Save data to files

– Read data from files

But we might also want to

– See what files we have

– Delete files

– Group these into directories

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

Python Browsing Directories

We can use Python to

– Save data to files

– Read data from files

But we might also want to

– See what files we have

– Delete files

– Group these into directories

– Structure these directories into a tree

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

Python Browsing Directories

We could use the shell

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

Python Browsing Directories

We could use the shell

Our program will be a mixture of

– Python

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

Python Browsing Directories

We could use the shell

Our program will be a mixture of

– Python

– Shell commands

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

Python Browsing Directories

We could use the shell

Our program will be a mixture of

– Python

– Shell commands

This is not portable

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

Python Browsing Directories

We could use the shell

Our program will be a mixture of

– Python

– Shell commands

This is not portable

Do it all in Python

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

Python Browsing Directories

>>> from os import getcwd Import getcwd

from the os module

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

Python Browsing Directories

>>> from os import getcwd

>>> getcwd()

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

Python Browsing Directories

>>> from os import getcwd

>>> getcwd()

'/users/vlad' Current working directory

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

Python Browsing Directories

>>> from os import getcwd

>>> getcwd()

'/users/vlad'

>>> originaldir = getcwd() Save the current working

directory in a variable

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

Python Browsing Directories

>>> from os import getcwd

>>> getcwd()

'/users/vlad'

>>> originaldir = getcwd()

>>> print originaldir Use the variable

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

Python Browsing Directories

>>> from os import getcwd

>>> getcwd()

'/users/vlad'

>>> originaldir = getcwd()

>>> print originaldir

/users/vlad

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

Python Browsing Directories

>>> from os import listdir

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

Python Browsing Directories

>>> from os import listdir

>>> listdir('.')

vlad

bin musicmail papersdata

solar

notes.txt

pizza.cfg solar.pdf swc

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

Python Browsing Directories

>>> from os import listdir

>>> listdir('.')

vlad

bin musicmail papersdata

solar

notes.txt

pizza.cfg solar.pdf swc

Current working directory

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

Python Browsing Directories

>>> from os import listdir

>>> listdir('.')

['solar', 'mail', 'pizza.cfg', 'notes.txt',

'swc', 'data', 'papers', 'solar.pdf',

'bin', 'music']vlad

bin musicmail papersdata

solar

notes.txt

pizza.cfg solar.pdf swc

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

Python Browsing Directories

>>> from os import listdir

>>> listdir('.')

['solar', 'mail', 'pizza.cfg', 'notes.txt',

'swc', 'data', 'papers', 'solar.pdf',

'bin', 'music']vlad

bin musicmail papersdata

solar

notes.txt

pizza.cfg solar.pdf swc

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

Python Browsing Directories

>>> listdir('.')

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

Python Browsing Directories

>>> listdir(getcwd())Use the result of getcwd

as the input directory

to listdir

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

Python Browsing Directories

>>> listdir(getcwd())

['solar', 'mail', 'pizza.cfg', 'notes.txt',

'swc', 'data', 'papers', 'solar.pdf',

'bin', 'music']

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

Python Browsing Directories

>>> listdir(originaldir)Use a variable as the

input directory to listdir

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

Python Browsing Directories

>>> listdir(originaldir)

['solar', 'mail', 'pizza.cfg', 'notes.txt',

'swc', 'data', 'papers', 'solar.pdf',

'bin', 'music']

Use a variable as the

input directory to listdir

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

Python Browsing Directories

>>> listdir(originaldir)

['solar', 'mail', 'pizza.cfg', 'notes.txt',

'swc', 'data', 'papers', 'solar.pdf',

'bin', 'music']

>>> files = listdir(originaldir)

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

Python Browsing Directories

>>> listdir(originaldir)

['solar', 'mail', 'pizza.cfg', 'notes.txt',

'swc', 'data', 'papers', 'solar.pdf',

'bin', 'music']

>>> files = listdir(originaldir)

>>> print files

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

Python Browsing Directories

>>> listdir(originaldir)

['solar', 'mail', 'pizza.cfg', 'notes.txt',

'swc', 'data', 'papers', 'solar.pdf',

'bin', 'music']

>>> files = listdir(originaldir)

>>> print files

['solar', 'mail', 'pizza.cfg', 'notes.txt',

'swc', 'data', 'papers', 'solar.pdf',

'bin', 'music']

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

Python Browsing Directories

>>> for file in files: Remember the colon

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

Python Browsing Directories

>>> for file in files:

... print file

Remember the 4 spaces

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

Python Browsing Directories

>>> for file in files:

... print file

...

Remember RETURN to close the loop

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

Python Browsing Directories

>>> for file in files:

... print file

...

solar

mail

pizza.cfg

notes.txt

swc

data

papers

solar.pdf

bin

music

vlad

bin musicmail papersdata

solar

notes.txt

pizza.cfg solar.pdf swc

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

Python Browsing Directories

>>> getcwd()

'/users/vlad'

>>> from os import chdir

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

Python Browsing Directories

>>> getcwd()

'/users/vlad'

>>> from os import chdir

>>> chdir('data')

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

Python Browsing Directories

>>> getcwd()

'/users/vlad'

>>> from os import chdir

>>> chdir('data')

>>> getcwd()

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

Python Browsing Directories

>>> getcwd()

'/users/vlad'

>>> from os import chdir

>>> chdir('data')

>>> getcwd()

'/users/vlad/data'chdir changes the current

working directory

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

Python Browsing Directories

>>> getcwd()

'/users/vlad'

>>> from os import chdir

>>> chdir('data')

>>> getcwd()

'/users/vlad/data'

>>> listdir(getcwd())

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

Python Browsing Directories

>>> getcwd()

'/users/vlad'

>>> from os import chdir

>>> chdir('data')

>>> getcwd()

'/users/vlad/data'

>>> listdir(getcwd())

['morse.txt', 'pdb', 'planets.txt',

'amino_acids.txt', 'elements', 'sunspot.txt']

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

Python Browsing Directories

>>> getcwd()

'/users/vlad'

>>> from os import chdir

>>> chdir('data')

>>> getcwd()

'/users/vlad/data'

>>> listdir(getcwd())

['morse.txt', 'pdb', 'planets.txt',

'amino_acids.txt', 'elements', 'sunspot.txt']

>>> chdir(originaldir)

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

Python Browsing Directories

>>> getcwd()

'/users/vlad'

>>> from os import chdir

>>> chdir('data')

>>> getcwd()

'/users/vlad/data'

>>> listdir(getcwd())

['morse.txt', 'pdb', 'planets.txt',

'amino_acids.txt', 'elements', 'sunspot.txt']

>>> chdir(originaldir)

>>> getcwd()

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

Python Browsing Directories

>>> getcwd()

'/users/vlad'

>>> from os import chdir

>>> chdir('data')

>>> getcwd()

'/users/vlad/data'

>>> listdir(getcwd())

['morse.txt', 'pdb', 'planets.txt',

'amino_acids.txt', 'elements', 'sunspot.txt']

>>> chdir(originaldir)

>>> getcwd()

'/users/vlad'

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

Python Browsing Directories

>>> chdir('data')

bin data users tmp

/

root

imhotep larry vlad

data

Page 48: Browsing Directories Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Browsing Directories

>>> chdir('data')

>>> getcwd()

bin data users tmp

/

root

imhotep larry vlad

data

Page 49: Browsing Directories Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Browsing Directories

>>> chdir('data')

>>> getcwd()

'/users/vlad/data'

bin data users tmp

/

root

imhotep larry vlad

data

What Python considers to be

the current working directory

Page 50: Browsing Directories Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Browsing Directories

>>> chdir('data')

>>> getcwd()

'/users/vlad/data'

>>> CTRL-D

bin data users tmp

/

root

imhotep larry vlad

data

What Python considers to be

the current working directory

Page 51: Browsing Directories Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Browsing Directories

>>> chdir('data')

>>> getcwd()

'/users/vlad/data'

>>> CTRL-D

$ pwd bin data users tmp

/

root

imhotep larry vlad

data

What Python considers to be

the current working directory

Page 52: Browsing Directories Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Browsing Directories

>>> chdir('data')

>>> getcwd()

'/users/vlad/data'

>>> CTRL-D

$ pwd

'/users/vlad'bin data users tmp

/

root

imhotep larry vlad

data

What Python considers to be

the current working directory

What the shell considers to be

the current working directory

Page 53: Browsing Directories Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution

Python Browsing Directories

os Miscellaneous operating system interfaces

getcwd Get current working directorylistdir List directory contentschdir Change directory

Page 54: Browsing Directories 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.