Blog do Zé Andrade

Software Livre, T.I., Eletrônica & Generalidades

Posts Tagged ‘python’

Python – emulando comandos shell

segunda-feira, janeiro 25th, 2010
#!/usr/bin/python
#-*- coding: UTF-8 -*-

import os

class Cmd(object):
   def __init__(self, cmd):
       self.cmd = cmd
   def __call__(self, *args):
       return os.system("%s %s" % (self.cmd, " ".join(args)))

class Sh(object):
    def __getattr__(self, attribute):
        return Cmd(attribute)

# exemplos de uso:

sh = Sh()
sh.ls('-la')
sh.free()
sh.ps('aux')
sh.uname('-a')

Python – ordenar linhas de um arquivo

segunda-feira, janeiro 25th, 2010

uma dica legal que en contrei em :

http://code.activestate.com/recipes/440612/

Um script em Pyhon para ordenar as linhas de um arquivo:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
map(sys.stdout.write, sorted(file(sys.argv[1]).readlines()))