Browsing Directories Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This...
-
Upload
jade-white -
Category
Documents
-
view
212 -
download
0
Transcript of Browsing Directories Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This...
![Page 1: Browsing Directories Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/1.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/2.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/4.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/5.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/6.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/7.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/8.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/9.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/10.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/11.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/12.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/14.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/15.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/16.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/17.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/18.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/19.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/20.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/21.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/22.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/23.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/24.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/25.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/26.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/27.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/28.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/29.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/30.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/31.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/32.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/33.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/34.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/35.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/36.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/37.jpg)
Python Browsing Directories
>>> for file in files:
... print file
...
solar
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/38.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/39.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/40.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/41.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/42.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/43.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/44.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/45.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/46.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/47.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/48.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/49.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/50.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/51.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/52.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/53.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022070415/56649efb5503460f94c0e173/html5/thumbnails/54.jpg)
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.