Mostly Linux & Python syntax notes and hyperlinks.

Wednesday, June 19, 2013

Veracode Infographic - History of Programming Languages

from a Tweet, to a website, to an offer to Add this Infographic to Your Website for FREE!

Infographic by Veracode Application Security

Tuesday, June 11, 2013

python: os.path pathname split examples

From http://docs.python.org/2/library/os.path.html,
>>> import os
>>> pth="/abeja/blanca/zumbas/ebria/de/miel/en/mi/alma/y.te.tuerces"

>>> os.path.split(pth)
('/abeja/blanca/zumbas/ebria/de/miel/en/mi/alma', 'y.te.tuerces')

>>> os.path.dirname(pth)
'/abeja/blanca/zumbas/ebria/de/miel/en/mi/alma'

>>> os.path.basename(pth)
'y.te.tuerces'

>>> os.path.splitext(os.path.basename(pth))
('y.te', '.tuerces')

>>> os.path.splitext(pth)
('/abeja/blanca/zumbas/ebria/de/miel/en/mi/alma/y.te', '.tuerces')

And then to get-last-directory-name-in-a-path

Tuesday, June 4, 2013

python: checking whether link exists os.path.exists(), os.path.lexists(),os.path.islink()

We were checking whether a link existed before trying to create it.
We were using os.path.exists(). This failed when there was a broken link.
It is better to use os.path.lexists() to check whether something exists before you use that name again for linking to another file.  (See also may2011)
$ l foo
-rw-rw-r-- 1 usr grp 8 Jul 11  2012 foo

$ ln -s foo fiBad
create symbolic link `fiBad' to `foo'
$ mv foo foooo
`foo' -> `foooo'
$ ln -s foooo fiGood
create symbolic link `fiGood' to `foooo'
$ l f*
-rw-rw-r-- 1
usr grp 8 Jul 11  2012 foooo
lrwxrwxrwx 1
usr grp 3 Jun  4 18:23 fiBad -> foo
lrwxrwxrwx 1
usr grp 5 Jun  4 18:24 fiGood -> foooo

$ python
Python 2.4.1 (#2, Jul 24 2007, 12:14:31)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-34)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> if os.path.exists("foooo"):
...   print "T"
...
T
>>> if os.path.exists("fiGood"):
...   print "T"
...
T
>>> if os.path.exists("fiBad"):
...   print "T"
... else:
...   print "F"
...
F
>>> if os.path.lexists("fiBad"):
...   print "T"
... else:
...   print "F"
...
T
>>> if os.path.lexists("foooo"):
...   print "T"
...
T
>>> if os.path.lexists("fiGood"):
...   print "T"
...
T
>>> if os.path.islink("fiGood"):
...   print "T"
...
T
>>> if os.path.islink("fiBad"):
...   print "T"
... else:
...   print "F"
...
T


Useful list of os.path methods here.