Download presentation
Presentation is loading. Please wait.
Published byJade White Modified over 9 years ago
1
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
2
Browsing Directories We can use Python to
3
PythonBrowsing Directories We can use Python to –Save data to files
4
PythonBrowsing Directories We can use Python to –Save data to files –Read data from files
5
PythonBrowsing Directories We can use Python to –Save data to files –Read data from files But we might also want to
6
PythonBrowsing 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
7
PythonBrowsing 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
8
PythonBrowsing 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
9
PythonBrowsing 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
10
PythonBrowsing Directories We could use the shell
11
PythonBrowsing Directories We could use the shell Our program will be a mixture of –Python
12
PythonBrowsing Directories We could use the shell Our program will be a mixture of –Python –Shell commands
13
PythonBrowsing Directories We could use the shell Our program will be a mixture of –Python –Shell commands This is not portable
14
PythonBrowsing 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
15
PythonBrowsing Directories >>> from os import getcwd Import getcwd from the os module
16
PythonBrowsing Directories >>> from os import getcwd >>> getcwd()
17
PythonBrowsing Directories >>> from os import getcwd >>> getcwd() '/users/vlad' Current working directory
18
PythonBrowsing Directories >>> from os import getcwd >>> getcwd() '/users/vlad' >>> originaldir = getcwd() Save the current working directory in a variable
19
PythonBrowsing Directories >>> from os import getcwd >>> getcwd() '/users/vlad' >>> originaldir = getcwd() >>> print originaldir Use the variable
20
PythonBrowsing Directories >>> from os import getcwd >>> getcwd() '/users/vlad' >>> originaldir = getcwd() >>> print originaldir /users/vlad
21
PythonBrowsing Directories >>> from os import listdir
22
PythonBrowsing Directories >>> from os import listdir >>> listdir('.') vlad binmusicmail papers data solar notes.txt pizza.cfg solar.pdf swc
23
PythonBrowsing Directories >>> from os import listdir >>> listdir('.') vlad binmusicmail papers data solar notes.txt pizza.cfg solar.pdf swc Current working directory
24
PythonBrowsing Directories >>> from os import listdir >>> listdir('.') ['solar', 'mail', 'pizza.cfg', 'notes.txt', 'swc', 'data', 'papers', 'solar.pdf', 'bin', 'music'] vlad binmusicmail papers data solar notes.txt pizza.cfg solar.pdf swc
25
PythonBrowsing Directories >>> from os import listdir >>> listdir('.') ['solar', 'mail', 'pizza.cfg', 'notes.txt', 'swc', 'data', 'papers', 'solar.pdf', 'bin', 'music'] vlad binmusicmail papers data solar notes.txt pizza.cfg solar.pdf swc
26
PythonBrowsing Directories >>> listdir('.')
27
PythonBrowsing Directories >>> listdir(getcwd()) Use the result of getcwd as the input directory to listdir
28
PythonBrowsing Directories >>> listdir(getcwd()) ['solar', 'mail', 'pizza.cfg', 'notes.txt', 'swc', 'data', 'papers', 'solar.pdf', 'bin', 'music']
29
PythonBrowsing Directories >>> listdir(originaldir) Use a variable as the input directory to listdir
30
PythonBrowsing 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
31
PythonBrowsing Directories >>> listdir(originaldir) ['solar', 'mail', 'pizza.cfg', 'notes.txt', 'swc', 'data', 'papers', 'solar.pdf', 'bin', 'music'] >>> files = listdir(originaldir)
32
PythonBrowsing Directories >>> listdir(originaldir) ['solar', 'mail', 'pizza.cfg', 'notes.txt', 'swc', 'data', 'papers', 'solar.pdf', 'bin', 'music'] >>> files = listdir(originaldir) >>> print files
33
PythonBrowsing 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']
34
PythonBrowsing Directories >>> for file in files:Remember the colon
35
PythonBrowsing Directories >>> for file in files:... print file Remember the 4 spaces
36
PythonBrowsing Directories >>> for file in files:... print file... Remember RETURN to close the loop
37
PythonBrowsing Directories >>> for file in files:... print file... solar mail pizza.cfg notes.txt swc data papers solar.pdf bin music vlad binmusicmail papers data solar notes.txt pizza.cfg solar.pdf swc
38
PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir
39
PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir >>> chdir('data')
40
PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir >>> chdir('data') >>> getcwd()
41
PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir >>> chdir('data') >>> getcwd() '/users/vlad/data' chdir changes the current working directory
42
PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir >>> chdir('data') >>> getcwd() '/users/vlad/data' >>> listdir(getcwd())
43
PythonBrowsing 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']
44
PythonBrowsing 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)
45
PythonBrowsing 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()
46
PythonBrowsing 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'
47
PythonBrowsing Directories >>> chdir('data') bindatauserstmp / root imhoteplarryvlad data
48
PythonBrowsing Directories >>> chdir('data') >>> getcwd() bindatauserstmp / root imhoteplarryvlad data
49
PythonBrowsing Directories >>> chdir('data') >>> getcwd() '/users/vlad/data' bindatauserstmp / root imhoteplarryvlad data What Python considers to be the current working directory
50
PythonBrowsing Directories >>> chdir('data') >>> getcwd() '/users/vlad/data' >>> CTRL-D bindatauserstmp / root imhoteplarryvlad data What Python considers to be the current working directory
51
PythonBrowsing Directories >>> chdir('data') >>> getcwd() '/users/vlad/data' >>> CTRL-D $ pwd bindatauserstmp / root imhoteplarryvlad data What Python considers to be the current working directory
52
PythonBrowsing Directories >>> chdir('data') >>> getcwd() '/users/vlad/data' >>> CTRL-D $ pwd '/users/vlad' bindatauserstmp / root imhoteplarryvlad data What Python considers to be the current working directory What the shell considers to be the current working directory
53
PythonBrowsing Directories os Miscellaneous operating system interfaces getcwd Get current working directory listdir List directory contents chdirChange directory
54
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.