05 September 2010

Common tasks in Python

Common tasks including:
  1. Traversing file system - os.listdir(dir)
  2. Getting file path name - 
    • os.path.join(dir, filename): return a path that puts dir and filename together
    • os.path.abspath(path): return the absolute path name of path
  3. Test file/directory existence - os.path.exists(path)
  4. Create directories - os.mkdir(dir_path)
  5. High level file operations - shutil module ; in particular file copying and removal
  6. Running other commands from within python program - commands module
    • commands.getstatusoutput(cmd) : return a tuple (status, output)
    • commands.getoutput(cmd)
  7. Downloading a web page from an url to a local file - urllib module:
    • urllib.retrieve(url, local_dst_path)
        Reference at:
        http://docs.python.org/library/commands.html
        http://docs.python.org/library/shutil.html
        http://code.google.com/edu/languages/google-python-class/utilities.html
        http://docs.python.org/library/urllib.html

        No comments: