Download presentation
Presentation is loading. Please wait.
Published byGyles Hawkins Modified over 9 years ago
1
Querying Directory Contents Copyright © The University of Edinburgh 2011 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Python
2
Querying Directory Contents We know how to move around directories
3
PythonQuerying Directory Contents We know how to move around directories And how to list what’s in directories
4
PythonQuerying Directory Contents We know how to move around directories And how to list what’s in directories How do we
5
PythonQuerying Directory Contents We know how to move around directories And how to list what’s in directories How do we –Check whether a file exists
6
PythonQuerying Directory Contents We know how to move around directories And how to list what’s in directories How do we –Check whether a file exists –Tell apart a file and a directory
7
PythonQuerying Directory Contents We know how to move around directories And how to list what’s in directories How do we –Check whether a file exists –Tell apart a file and a directory –See if two variables refer to the same directory
8
PythonQuerying Directory Contents We know how to move around directories And how to list what’s in directories How do we –Check whether a file exists –Tell apart a file and a directory –See if two variables refer to the same directory – Check that we are allowed to delete a file
9
PythonQuerying Directory Contents We know how to move around directories And how to list what’s in directories How do we –Check whether a file exists –Tell apart a file and a directory –See if two variables refer to the same directory – Check that we are allowed to delete a file – Get the size of a file
10
PythonQuerying Directory Contents >>> from os import exists pizza.cfg notes.txt solar vladusers
11
PythonQuerying Directory Contents >>> from os import exists >>> exists('pizza.cfg') pizza.cfg notes.txt solar vladusers
12
PythonQuerying Directory Contents >>> from os import exists >>> exists('pizza.cfg') True pizza.cfg notes.txt solar vladusers
13
PythonQuerying Directory Contents >>> from os import exists >>> exists('pizza.cfg') True >>> exists('/users/vlad/pizza.cfg') pizza.cfg notes.txt solar vladusers
14
PythonQuerying Directory Contents >>> from os import exists >>> exists('pizza.cfg') True >>> exists('/users/vlad/pizza.cfg') True pizza.cfg notes.txt solar vladusers
15
PythonQuerying Directory Contents >>> from os import exists >>> exists('pizza.cfg') True >>> exists('/users/vlad/pizza.cfg') True >>> exists('solar') pizza.cfg notes.txt solar vladusers
16
PythonQuerying Directory Contents >>> from os import exists >>> exists('pizza.cfg') True >>> exists('/users/vlad/pizza.cfg') True >>> exists('solar') True pizza.cfg notes.txt solar vladusers
17
PythonQuerying Directory Contents >>> from os import exists >>> exists('pizza.cfg') True >>> exists('/users/vlad/pizza.cfg') True >>> exists('solar') True >>> exists('/users/vlad/solar') pizza.cfg notes.txt solar vladusers
18
PythonQuerying Directory Contents >>> from os import exists >>> exists('pizza.cfg') True >>> exists('/users/vlad/pizza.cfg') True >>> exists('solar') True >>> exists('/users/vlad/solar') True pizza.cfg notes.txt solar vladusers
19
PythonQuerying Directory Contents >>> from os import exists >>> exists('pizza.cfg') True >>> exists('/users/vlad/pizza.cfg') True >>> exists('solar') True >>> exists('/users/vlad/solar') True >>> exists('no-such-file') pizza.cfg notes.txt solar vladusers
20
PythonQuerying Directory Contents >>> from os import exists >>> exists('pizza.cfg') True >>> exists('/users/vlad/pizza.cfg') True >>> exists('solar') True >>> exists('/users/vlad/solar') True >>> exists('no-such-file') False pizza.cfg notes.txt solar vladusers
21
PythonQuerying Directory Contents >>> from os import exists >>> exists('pizza.cfg') True >>> exists('/users/vlad/pizza.cfg') True >>> exists('solar') True >>> exists('/users/vlad/solar') True >>> exists('no-such-file') False >>> exists('/users/vlad/no-such-thing') pizza.cfg notes.txt solar vladusers
22
PythonQuerying Directory Contents >>> from os import exists >>> exists('pizza.cfg') True >>> exists('/users/vlad/pizza.cfg') True >>> exists('solar') True >>> exists('/users/vlad/solar') True >>> exists('no-such-file') False >>> exists('/users/vlad/no-such-thing') False pizza.cfg notes.txt solar vladusers
23
PythonQuerying Directory Contents >>> from os import isfile, isdir pizza.cfg notes.txt solar vladusers
24
PythonQuerying Directory Contents >>> from os import isfile, isdir >>> isfile('pizza.cfg') pizza.cfg notes.txt solar vladusers
25
PythonQuerying Directory Contents >>> from os import isfile, isdir >>> isfile('pizza.cfg') True pizza.cfg notes.txt solar vladusers
26
PythonQuerying Directory Contents >>> from os import isfile, isdir >>> isfile('pizza.cfg') True >>> isfile('solar') pizza.cfg notes.txt solar vladusers
27
PythonQuerying Directory Contents >>> from os import isfile, isdir >>> isfile('pizza.cfg') True >>> isfile('solar') False pizza.cfg notes.txt solar vladusers
28
PythonQuerying Directory Contents >>> from os import isfile, isdir >>> isfile('pizza.cfg') True >>> isfile('solar') False >>> isfile('no-such-file') pizza.cfg notes.txt solar vladusers
29
PythonQuerying Directory Contents >>> from os import isfile, isdir >>> isfile('pizza.cfg') True >>> isfile('solar') False >>> isfile('no-such-file') False pizza.cfg notes.txt solar vladusers
30
PythonQuerying Directory Contents >>> from os import isfile, isdir >>> isfile('pizza.cfg') True >>> isfile('solar') False >>> isfile('no-such-file') False >>> isdir('solar') pizza.cfg notes.txt solar vladusers
31
PythonQuerying Directory Contents >>> from os import isfile, isdir >>> isfile('pizza.cfg') True >>> isfile('solar') False >>> isfile('no-such-file') False >>> isdir('solar') True pizza.cfg notes.txt solar vladusers
32
PythonQuerying Directory Contents >>> from os import isfile, isdir >>> isfile('pizza.cfg') True >>> isfile('solar') False >>> isfile('no-such-file') False >>> isdir('solar') True >>> isdir('pizza.cfg') pizza.cfg notes.txt solar vladusers
33
PythonQuerying Directory Contents >>> from os import isfile, isdir >>> isfile('pizza.cfg') True >>> isfile('solar') False >>> isfile('no-such-file') False >>> isdir('solar') True >>> isdir('pizza.cfg') False pizza.cfg notes.txt solar vladusers
34
PythonQuerying Directory Contents >>> from os import isfile, isdir >>> isfile('pizza.cfg') True >>> isfile('solar') False >>> isfile('no-such-file') False >>> isdir('solar') True >>> isdir('pizza.cfg') False >>> isdir('no-such-dir') pizza.cfg notes.txt solar vladusers
35
PythonQuerying Directory Contents >>> from os import isfile, isdir >>> isfile('pizza.cfg') True >>> isfile('solar') False >>> isfile('no-such-file') False >>> isdir('solar') True >>> isdir('pizza.cfg') False >>> isdir('no-such-dir') False pizza.cfg notes.txt solar vladusers
36
PythonQuerying Directory Contents >>> def check(path):... if isfile(path):... print "It's a file"... elif isdir(path):... print "It's a directory"... else:... print "It doesn't exist"...
37
PythonQuerying Directory Contents >>> def check(path):... if isfile(path):... print "It's a file"... elif isdir(path):... print "It's a directory"... else:... print "It doesn't exist"...
38
PythonQuerying Directory Contents >>> def check(path):... if isfile(path):... print "It's a file"... elif isdir(path):... print "It's a directory"... else:... print "It doesn't exist"...
39
PythonQuerying Directory Contents >>> def check(path):... if isfile(path):... print "It's a file"... elif isdir(path):... print "It's a directory"... else:... print "It doesn't exist"...
40
PythonQuerying Directory Contents >>> def check(path):... if isfile(path):... print "It's a file"... elif isdir(path):... print "It's a directory"... else:... print "It doesn't exist"... >>> check('/users/vlad/pizza.cfg') pizza.cfg notes.txt solar vladusers
41
PythonQuerying Directory Contents >>> def check(path):... if isfile(path):... print "It's a file"... elif isdir(path):... print "It's a directory"... else:... print "It doesn't exist"... >>> check('/users/vlad/pizza.cfg') It's a file pizza.cfg notes.txt solar vladusers
42
PythonQuerying Directory Contents >>> def check(path):... if isfile(path):... print "It's a file"... elif isdir(path):... print "It's a directory"... else:... print "It doesn't exist"... >>> check('/users/vlad/pizza.cfg') It's a file >>> check('/users/vlad/solar') pizza.cfg notes.txt solar vladusers
43
PythonQuerying Directory Contents >>> def check(path):... if isfile(path):... print "It's a file"... elif isdir(path):... print "It's a directory"... else:... print "It doesn't exist"... >>> check('/users/vlad/pizza.cfg') It's a file >>> check('/users/vlad/solar') It's a directory pizza.cfg notes.txt solar vladusers
44
PythonQuerying Directory Contents >>> def check(path):... if isfile(path):... print "It's a file"... elif isdir(path):... print "It's a directory"... else:... print "It doesn't exist"... >>> check('/users/vlad/pizza.cfg') It's a file >>> check('/users/vlad/solar') It's a directory >>> check('/users/vlad/no-such-file') pizza.cfg notes.txt solar vladusers
45
PythonQuerying Directory Contents >>> def check(path):... if isfile(path):... print "It's a file"... elif isdir(path):... print "It's a directory"... else:... print "It doesn't exist"... >>> check('/users/vlad/pizza.cfg') It's a file >>> check('/users/vlad/solar') It's a directory >>> check('/users/vlad/no-such-file') It doesn't exist pizza.cfg notes.txt solar vladusers
46
PythonQuerying Directory Contents >>> from os.path import samefile pizza.cfg notes.txt solar vladusers
47
PythonQuerying Directory Contents >>> from os.path import samefile >>> file1 = 'pizza.cfg' >>> file2 = '/users/vlad/pizza.cfg' >>> file3 = 'notes.txt' pizza.cfg notes.txt solar vladusers file3file1file2
48
PythonQuerying Directory Contents >>> from os.path import samefile >>> file1 = 'pizza.cfg' >>> file2 = '/users/vlad/pizza.cfg' >>> file3 = 'notes.txt' >>> samefile(file1, file2) pizza.cfg notes.txt solar vladusers file1file2file3
49
PythonQuerying Directory Contents >>> from os.path import samefile >>> file1 = 'pizza.cfg' >>> file2 = '/users/vlad/pizza.cfg' >>> file3 = 'notes.txt' >>> samefile(file1, file2) True pizza.cfg notes.txt solar vladusers file3file1file2
50
PythonQuerying Directory Contents >>> from os.path import samefile >>> file1 = 'pizza.cfg' >>> file2 = '/users/vlad/pizza.cfg' >>> file3 = 'notes.txt' >>> samefile(file1, file2) True >>> samefile(file1, file3) pizza.cfg notes.txt solar vladusers file3file1file2
51
PythonQuerying Directory Contents >>> from os.path import samefile >>> file1 = 'pizza.cfg' >>> file2 = '/users/vlad/pizza.cfg' >>> file3 = 'notes.txt' >>> samefile(file1, file2) True >>> samefile(file1, file3) False pizza.cfg notes.txt solar vladusers file2file3file1
52
PythonQuerying Directory Contents >>> from os import access
53
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK
54
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) Does the path exist?
55
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) True Does the path exist?
56
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) True >>> access('pizza.cfg', R_OK) Can it be read?
57
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) True >>> access('pizza.cfg', R_OK) True Can it be read?
58
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) True >>> access('pizza.cfg', R_OK) True >>> access('pizza.cfg', W_OK) Can it be written?
59
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) True >>> access('pizza.cfg', R_OK) True >>> access('pizza.cfg', W_OK) True Can it be written?
60
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) True >>> access('pizza.cfg', R_OK) True >>> access('pizza.cfg', W_OK) True >>> access('pizza.cfg', X_OK) Can it be executed?
61
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) True >>> access('pizza.cfg', R_OK) True >>> access('pizza.cfg', W_OK) True >>> access('pizza.cfg', X_OK) False Can it be executed?
62
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) True >>> access('pizza.cfg', R_OK) True >>> access('pizza.cfg', W_OK) True >>> access('pizza.cfg', X_OK) False >>> access('pizza.cfg', os.R_OK | os.W_OK)
63
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) True >>> access('pizza.cfg', R_OK) True >>> access('pizza.cfg', W_OK) True >>> access('pizza.cfg', X_OK) False >>> access('pizza.cfg', os.R_OK | os.W_OK) True
64
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) True >>> access('pizza.cfg', R_OK) True >>> access('pizza.cfg', W_OK) True >>> access('pizza.cfg', X_OK) False >>> access('pizza.cfg', os.R_OK | os.W_OK) True >>> access('pizza.cfg', os.R_OK | os.X_OK)
65
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) True >>> access('pizza.cfg', R_OK) True >>> access('pizza.cfg', W_OK) True >>> access('pizza.cfg', X_OK) False >>> access('pizza.cfg', os.R_OK | os.W_OK) True >>> access('pizza.cfg', os.R_OK | os.X_OK) False
66
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) True >>> access('pizza.cfg', R_OK) True >>> access('pizza.cfg', W_OK) True >>> access('pizza.cfg', X_OK) False >>> access('pizza.cfg', os.R_OK | os.W_OK) True >>> access('pizza.cfg', os.R_OK | os.X_OK) False >>> access('pizza.cfg', os.F_OK | os.R_OK | os.W_OK)
67
PythonQuerying Directory Contents >>> from os import access >>> from os import F_OK, R_OK, W_OK, X_OK >>> access('pizza.cfg', F_OK) True >>> access('pizza.cfg', R_OK) True >>> access('pizza.cfg', W_OK) True >>> access('pizza.cfg', X_OK) False >>> access('pizza.cfg', os.R_OK | os.W_OK) True >>> access('pizza.cfg', os.R_OK | os.X_OK) False >>> access('pizza.cfg', os.F_OK | os.R_OK | os.W_OK) True
68
PythonQuerying Directory Contents >>> from os import stat >>> info = stat('pizza.cfg')
69
PythonQuerying Directory Contents >>> from os import stat >>> info = stat('pizza.cfg') >>> print info (33188, 5557L, 73188003L, 1, 3946, 4000, 8172L, 1304999431, 1304951325, 1304951325) stat returns a record
70
PythonQuerying Directory Contents >>> from os import stat >>> info = stat('pizza.cfg') >>> print info (33188, 5557L, 73188003L, 1, 3946, 4000, 8172L, 1304999431, 1304951325, 1304951325) >>> print info.st_mode 33188 Protection bits
71
PythonQuerying Directory Contents >>> from os import stat >>> info = stat('pizza.cfg') >>> print info (33188, 5557L, 73188003L, 1, 3946, 4000, 8172L, 1304999431, 1304951325, 1304951325) >>> print info.st_mode 33188 >>> print info.st_ino 5557 Inode number
72
PythonQuerying Directory Contents >>> from os import stat >>> info = stat('pizza.cfg') >>> print info (33188, 5557L, 73188003L, 1, 3946, 4000, 8172L, 1304999431, 1304951325, 1304951325) >>> print info.st_mode 33188 >>> print info.st_ino 5557 >>> print info.st_dev 73188003 Device
73
PythonQuerying Directory Contents >>> from os import stat >>> info = stat('pizza.cfg') >>> print info (33188, 5557L, 73188003L, 1, 3946, 4000, 8172L, 1304999431, 1304951325, 1304951325) >>> print info.st_mode 33188 >>> print info.st_ino 5557 >>> print info.st_dev 73188003 >>> print info.st_nlink 1 Number of hard links
74
PythonQuerying Directory Contents >>> from os import stat >>> info = stat('pizza.cfg') >>> print info (33188, 5557L, 73188003L, 1, 3946, 4000, 8172L, 1304999431, 1304951325, 1304951325) >>> print info.st_mode 33188 >>> print info.st_ino 5557 >>> print info.st_dev 73188003 >>> print info.st_nlink 1 >>> print info.st_uid 3946 Owner’s user ID
75
PythonQuerying Directory Contents >>> print info.st_gid 4000 Owner’s group ID
76
PythonQuerying Directory Contents >>> print info.st_gid 4000 >>> print info.st_size 8172 File size in bytes
77
PythonQuerying Directory Contents >>> print info.st_gid 4000 >>> print info.st_size 8172 >>> print info.st_atime 1304999431 Most recent access time
78
PythonQuerying Directory Contents >>> print info.st_gid 4000 >>> print info.st_size 8172 >>> print info.st_atime 1304999431 >>> print info.st_mtime 1304951325 Most recent modification
79
PythonQuerying Directory Contents >>> print info.st_gid 4000 >>> print info.st_size 8172 >>> print info.st_atime 1304999431 >>> print info.st_mtime 1304951325 >>> print info.st_ctime 1304951325 Most recent metadata change time for Linux or, creation time for Windows
80
PythonQuerying Directory Contents >>> print info.st_gid 4000 >>> print info.st_size 8172 >>> print info.st_atime 1304999431 >>> print info.st_mtime 1304951325 >>> print info.st_ctime 1304951325 >>> from os import stat_float_times >>> stat_float_times() False Owner’s group ID File size in bytes Most recent access time Most recent modification Most recent metadata change time for Linux or, creation time for Windows
81
PythonQuerying Directory Contents A stat record may contain operating system-specific values
82
PythonQuerying Directory Contents >>> print info.st_blocks 17 A stat record may contain operating system-specific values Number of blocks, for Linux
83
PythonQuerying Directory Contents >>> print info.st_blocks 17 >>> print info.st_blksize 8192 A stat record may contain operating system-specific values File system block size for Linux
84
PythonQuerying Directory Contents os.path Common pathname manipulations exists Does the given path exist? isfile Is the given path a file? isdir Is the given path a directory? samefile Are the given arguments paths to the same file or directory? os Miscellaneous operating system interfaces access Can the file/directory be accessed? statGet operating system-specific information
85
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.