Created
April 21, 2014 05:34
-
-
Save fengs/11133189 to your computer and use it in GitHub Desktop.
Usage of os.path method of the os module. From http://www.diveintopython3.net/comprehensions.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import os | |
>>> print(os.path.join('/Users/pilgrim/diveintopython3/examples/', 'humansize.py')) | |
/Users/pilgrim/diveintopython3/examples/humansize.py | |
>>> print(os.path.join('/Users/pilgrim/diveintopython3/examples', 'humansize.py')) | |
/Users/pilgrim/diveintopython3/examples\humansize.py | |
>>> print(os.path.expanduser('~')) | |
c:\Users\pilgrim | |
>>> print(os.path.join(os.path.expanduser('~'), 'diveintopython3', 'examples', 'humansize.py')) | |
c:\Users\pilgrim\diveintopython3\examples\humansize.py | |
>>> os.getcwd() | |
/home/you | |
>>> os.path.abspath('') | |
/home/you | |
>>> os.path.abspath('.ssh') | |
/home/you/.ssh | |
>>> pathname = '/Users/pilgrim/diveintopython3/examples/humansize.py' | |
>>> os.path.split(pathname) | |
('/Users/pilgrim/diveintopython3/examples', 'humansize.py') | |
>>> (dirname, filename) = os.path.split(pathname) | |
>>> dirname | |
'/Users/pilgrim/diveintopython3/examples' | |
>>> filename | |
'humansize.py' | |
>>> (shortname, extension) = os.path.splitext(filename) | |
>>> shortname | |
'humansize' | |
>>> extension | |
'.py' | |
>>> os.chdir('/Users/pilgrim/diveintopython3/') | |
>>> import glob | |
>>> glob.glob('examples/*.xml') ① | |
['examples\\feed-broken.xml', | |
'examples\\feed-ns0.xml', | |
'examples\\feed.xml'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment