querying directory contents copyright © the university of edinburgh 2011 this work is licensed...

85
Querying Directory Contents Copyright © The University of Edinburgh 2011 This work is licensed under the Creative Commons Attribution Lice See http://software-carpentry.org/license.html for more informati Python

Upload: gyles-hawkins

Post on 21-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Slide 1This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.
Python
*
Hello and welcome to the third episode of the Software Carpentry lectures on handling directories and files in Python. Here, we’ll continue to look at how we can explore directories by looking at the ways in which Python allows us to find out more about the contents of directories.
Python
*
Python
And how to list what’s in directories
*
Python
And how to list what’s in directories
How do we
*
But there are many other things we might want to find out.
Python
And how to list what’s in directories
How do we
*
We might want to check whether a file or directory already exists. This can be useful before saving a file to allow the user to decide if the file is to be overwritten.
Python
And how to list what’s in directories
How do we
– Tell apart a file and a directory
*
We may have a variable and want to see whether that refers to a file or a directory.
Python
And how to list what’s in directories
How do we
– Tell apart a file and a directory
– See if two variables refer to the same directory
*
We may want to see if two variables refer to the same file or directory.
Python
And how to list what’s in directories
How do we
– 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
*
We may want to find out what we can do with a file or directory. Are we allowed to read it, to update it, or delete it?
Python
And how to list what’s in directories
How do we
– 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
*
And, we may want to get information such as the file size, who owns it and when it was last modified.
Python
pizza.cfg
notes.txt
solar
vlad
users
A simple check we often want to do is to see whether a file or directory exists.
Python provides the exists function that does this check.
*
*
*
*
*
*
pizza.cfg
notes.txt
solar
vlad
users
*
>>> isfile('pizza.cfg')
pizza.cfg
notes.txt
solar
vlad
users
*
>>> isfile('pizza.cfg')
True
pizza.cfg
notes.txt
solar
vlad
users
*
>>> isfile('pizza.cfg')
True
>>> isfile('solar')
pizza.cfg
notes.txt
solar
vlad
users
*
>>> isfile('pizza.cfg')
True
>>> isfile('solar')
False
pizza.cfg
notes.txt
solar
vlad
users
*
>>> isfile('pizza.cfg')
True
>>> isfile('solar')
False
>>> isfile('no-such-file')
pizza.cfg
notes.txt
solar
vlad
users
*
>>> isfile('pizza.cfg')
True
>>> isfile('solar')
False
>>> isfile('no-such-file')
False
pizza.cfg
notes.txt
solar
vlad
users
>>> isfile('pizza.cfg')
True
>>> isfile('solar')
False
>>> isfile('no-such-file')
False
>>> isdir('solar')
pizza.cfg
notes.txt
solar
vlad
users
*
>>> isfile('pizza.cfg')
True
>>> isfile('solar')
False
>>> isfile('no-such-file')
False
>>> isdir('solar')
True
pizza.cfg
notes.txt
solar
vlad
users
>>> isfile('pizza.cfg')
True
>>> isfile('solar')
False
>>> isfile('no-such-file')
False
>>> isdir('solar')
True
>>> isdir('pizza.cfg')
pizza.cfg
notes.txt
solar
vlad
users
>>> isfile('pizza.cfg')
True
>>> isfile('solar')
False
>>> isfile('no-such-file')
False
>>> isdir('solar')
True
>>> isdir('pizza.cfg')
False
pizza.cfg
notes.txt
solar
vlad
users
>>> 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
vlad
users
*
>>> 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
vlad
users
...
*
>>> check('/users/vlad/pizza.cfg')
pizza.cfg
notes.txt
solar
vlad
users
*
*
*
pizza.cfg
notes.txt
solar
vlad
users
*
*
pizza.cfg
notes.txt
file1
file2
file3
solar
vlad
users
*
*
pizza.cfg
notes.txt
file3
file1
file2
solar
vlad
users
*
>>> from os import access
*
>>> from os import F_OK, R_OK, W_OK, X_OK
*
>>> access('pizza.cfg', F_OK)
Does the path exist?
*
>>> access('pizza.cfg', F_OK)
>>> access('pizza.cfg', F_OK)
Can it be read?
*
>>> access('pizza.cfg', F_OK)
>>> access('pizza.cfg', F_OK)
Can it be written?
*
>>> access('pizza.cfg', F_OK)
>>> access('pizza.cfg', F_OK)
Can it be executed?
*
>>> access('pizza.cfg', F_OK)
>>> access('pizza.cfg', F_OK)
>>> access('pizza.cfg', os.R_OK | os.W_OK)
*
>>> access('pizza.cfg', F_OK)
>>> access('pizza.cfg', F_OK)
>>> access('pizza.cfg', os.R_OK | os.X_OK)
*
>>> access('pizza.cfg', F_OK)
>>> access('pizza.cfg', F_OK)
>>> access('pizza.cfg', os.F_OK | os.R_OK | os.W_OK)
*
>>> access('pizza.cfg', F_OK)
>>> info = stat('pizza.cfg')
*
1304951325, 1304951325)
1304951325, 1304951325)
>>> print info.st_mode
This includes.
1304951325, 1304951325)
>>> print info.st_mode
1304951325, 1304951325)
>>> print info.st_mode
1304951325, 1304951325)
>>> print info.st_mode
1304951325, 1304951325)
>>> print info.st_mode
*
*
or, creation time for Windows
*
or, creation time for Windows
*
The stat record may also contain operating system-specific information.
*
Number of blocks, for Linux
*
File system block size for Linux
And the file system block size.
*
isfile
isdir
samefile
Are the given arguments paths to the same file or directory?
os
stat
Get operating system-specific information
*
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.
*