Python-Ref > System interaction > Manipulating files and directories > Looking around
 
 

<-^^->
Moduly
Knihovní funkce

Looking around

How to list a directory, find out if a file or directory exists and more.
The module os contains very useful submodule os.path that contains many convenience functions for querying of the filesystem.
The code below shows how to list a dir and find out what kind of object a path represents.
Expand/Shrink
Prostředí:
.
|-- CSS
|   |-- bullet.png
|   |-- bullet2.png
|   |-- bullet3.png
|   `-- style.css
|-- about_en.html
|-- conversion_info_en.html
|-- converter_en.html
|-- empty.html
|-- filesystem3-1.py
|-- img
|   |-- emacs.png
|   |-- header.gif
|   |-- header.png
|   |-- header.psd
|   |-- header.xcf
|   `-- rss.gif
|-- inchi_comparison_en.html
|-- inchi_mod.py
|-- inchi_overview_en.html
|-- inchi_to_any_en.html
|-- index.html
|-- js
|   |-- awstats_misc_tracker.js
|   `-- inchi.js
|-- links_en.html
|-- log
|   `-- submitted_inchis.log
|-- news.rss
|-- oasa -> /home/beda/oasa-devel/oasa
|-- pokus.py
|-- pokus_en.html
|-- smiles_to_any_en.html
|-- software_en.html
`-- tmp
  1   import os
  2   
  3   # list everything in the current directory
  4   for path in os.listdir( "./"):
  5       print "%-30s" % path,
  6       if os.path.islink( path):
  7           # path is a link
  8           print "%-20s" % "link",
  9       if os.path.isdir( path):
 10           # path is a directory
 11           print "%-20s" % "directory",
 12       if os.path.isfile( path):
 13           # file
 14           print "%-20s" % "file (%d B)" % os.path.getsize( path),
 15       print
stdout:
software_en.html               file (5741 B)         
filesystem3-1.py               file (413 B)         
empty.html                     file (42 B)         
index.html                     file (6004 B)         
links_en.html                  file (3014 B)         
oasa                           link                 directory           
js                             directory           
inchi_overview_en.html         file (5657 B)         
log                            directory           
pokus_en.html                  file (4763 B)         
inchi_mod.py                   file (5289 B)         
conversion_info_en.html        file (4786 B)         
news.rss                       file (1997 B)         
about_en.html                  file (3254 B)         
tmp                            directory           
img                            directory           
inchi_to_any_en.html           file (3115 B)         
smiles_to_any_en.html          file (3124 B)         
CSS                            directory           
inchi_comparison_en.html       file (6091 B)         
converter_en.html              file (4930 B)         
pokus.py                       file (336 B)         
Doba běhu: 35.1 ms
When listing a directory other than the current, one has to be careful when accessing the listed files - the names returned by os.listdir are only filenames and must be joined with the proper directory to work.
Expand/Shrink
Prostředí:
.
|-- CSS
|   |-- bullet.png
|   |-- bullet2.png
|   |-- bullet3.png
|   `-- style.css
|-- about_en.html
|-- conversion_info_en.html
|-- converter_en.html
|-- empty.html
|-- filesystem3-6.py
|-- img
|   |-- emacs.png
|   |-- header.gif
|   |-- header.png
|   |-- header.psd
|   |-- header.xcf
|   `-- rss.gif
|-- inchi_comparison_en.html
|-- inchi_mod.py
|-- inchi_overview_en.html
|-- inchi_to_any_en.html
|-- index.html
|-- js
|   |-- awstats_misc_tracker.js
|   `-- inchi.js
|-- links_en.html
|-- log
|   `-- submitted_inchis.log
|-- news.rss
|-- oasa -> /home/beda/oasa-devel/oasa
|-- pokus.py
|-- pokus_en.html
|-- smiles_to_any_en.html
|-- software_en.html
`-- tmp
  1   import os
  2   
  3   # first try
  4   for path in os.listdir( "img"):
  5       print "%-20s" % path,
  6       if os.path.exists( path):
  7           print "exists"
  8       else:
  9           print "does not exist"
 10   
 11   print
 12   print "And now for something completely different."
 13   print 
 14   
 15   # second try
 16   for name in os.listdir( "img"):
 17       path = os.path.join( "img", name)
 18       print "%-20s" % path,
 19       if os.path.exists( path):
 20           print "exists"
 21       else:
 22           print "does not exist"
stdout:
emacs.png            does not exist
rss.gif              does not exist
header.gif           does not exist
header.png           does not exist
header.psd           does not exist
header.xcf           does not exist

And now for something completely different.

img/emacs.png        exists
img/rss.gif          exists
img/header.gif       exists
img/header.png       exists
img/header.psd       exists
img/header.xcf       exists
Doba běhu: 42.2 ms
The following code shows how to check the existence of an object.
Expand/Shrink
Prostředí:
.
|-- filesystem3-2.py
|-- something
|   `-- example.py
|-- something_else
`-- test
    `-- test.txt
  1   import os
  2   
  3   names = ["test", "example", "something", "something.else", "index.html"]
  4   
  5   for name in names:
  6       if os.path.exists( name):
  7           print os.path.abspath( name)
  8       else:
  9           print "!! does not exist:", name
stdout:
/mnt/bkchem.img/projects/python-ref/_tmp/test
!! does not exist: example
/mnt/bkchem.img/projects/python-ref/_tmp/something
!! does not exist: something.else
!! does not exist: index.html
Doba běhu: 33.5 ms
Checking the extension of a file is a quite common task. How to do it shows the example bellow.
Expand/Shrink
Prostředí:
.
|-- CSS
|   |-- bullet.png
|   |-- bullet2.png
|   |-- bullet3.png
|   `-- style.css
|-- about_en.html
|-- conversion_info_en.html
|-- converter_en.html
|-- empty.html
|-- filesystem3-3.py
|-- img
|   |-- emacs.png
|   |-- header.gif
|   |-- header.png
|   |-- header.psd
|   |-- header.xcf
|   `-- rss.gif
|-- inchi_comparison_en.html
|-- inchi_mod.py
|-- inchi_overview_en.html
|-- inchi_to_any_en.html
|-- index.html
|-- js
|   |-- awstats_misc_tracker.js
|   `-- inchi.js
|-- links_en.html
|-- log
|   `-- submitted_inchis.log
|-- news.rss
|-- oasa -> /home/beda/oasa-devel/oasa
|-- pokus.py
|-- pokus_en.html
|-- smiles_to_any_en.html
|-- software_en.html
`-- tmp
  1   import os
  2   
  3   for path in os.listdir( "./"):
  4       if os.path.isfile( path):
  5           base, ext = os.path.splitext( path)
  6           print "basename = %-20s extension = %-20s" % (base, ext)
stdout:
basename = filesystem3-3        extension = .py                 
basename = about_en             extension = .html               
basename = conversion_info_en   extension = .html               
basename = converter_en         extension = .html               
basename = empty                extension = .html               
basename = inchi_comparison_en  extension = .html               
basename = inchi_mod            extension = .py                 
basename = inchi_overview_en    extension = .html               
basename = inchi_to_any_en      extension = .html               
basename = index                extension = .html               
basename = links_en             extension = .html               
basename = news                 extension = .rss                
basename = pokus                extension = .py                 
basename = pokus_en             extension = .html               
basename = smiles_to_any_en     extension = .html               
basename = software_en          extension = .html               
Doba běhu: 36.1 ms
Conversion of a path to an absolute one and splitting of the directory and filename part of path are shown below.
Expand/Shrink
  1   import os
  2   
  3   paths = ["/home/beda/example.txt", "../../test.txt", "something.xml"]
  4   
  5   for path in paths:
  6       dir, filename = os.path.split( path)
  7       base, ext = os.path.splitext( filename)
  8       print os.path.abspath( path), (dir, base, ext)
stdout:
/home/beda/example.txt ('/home/beda', 'example', '.txt')
/mnt/bkchem.img/projects/test.txt ('../..', 'test', '.txt')
/mnt/bkchem.img/projects/python-ref/_tmp/something.xml ('', 'something', '.xml')
Doba běhu: 21.3 ms
Sometimes it is useful to normalize a path.
Expand/Shrink
  1   import os
  2   
  3   paths = ["/home///beda/example.txt", ".././test.txt", "./something.xml"]
  4   
  5   for path in paths:
  6       print os.path.normpath( path)
stdout:
/home/beda/example.txt
../test.txt
something.xml
Doba běhu: 21.0 ms