[Ma-linux] Scripting redux Approaching
Bruce Israel
israel at tux.org
Sat Mar 12 02:37:58 EST 2011
Boy, seems like these lists have been pretty dead, except for
various meetings and conferences announcements.
[ Alan, you can stop reading now. ]
So I figured I'd share a useful script that I wrote recently.
I keep on running into the issue where I have commands or scripts
that take filenames and they need to be able to manipulate the
filenames properly (sometimes sending them to something else that
may not be working off of the same directory, or things like that),
so I wanted to be able to consistently and correctly convert them to
their full names. Previously when I did that, I'd just prepend the
current working directory if a relative directory, but when I needed
to extract the parent directory name, that didn't work (after all,
the simple parent directory name of /etc/./passwd ends up as ".").
So I put together the following python script that does it all
correctly and in a standardized fashion, and thought I'd share it
here if anyone else might find it useful. Enjoy.
Bruce
#!/usr/bin/env python
#
# fullname.py - get the full name of a file
# Written by: Bruce Israel <israel at tux.org>, Wed Feb 23 2011
#
# Usage: fullname.py <file> ...
#
# fullname handles both relative and absolute file addressing, e.g.
# cd /etc; fullname ./passwd ==> /etc/passwd
# fullname /tmp/../etc/./passwd ==> /etc/passwd
import sys
import os
def usage(prog):
print "Usage: %s <files>..." % (prog)
sys.exit(1)
def fullname(file, workingDir):
if not os.path.isabs(file):
file = "%s/%s" % (workingDir, file)
return os.path.normpath(file)
def main(args):
args.pop(0)
if len(args) == 0: usage(prog)
for file in args:
print fullname(file, os.getcwd())
if __name__ == '__main__':
main(sys.argv)
More information about the Ma-linux
mailing list