0

I need to run system command from python

I have python - version - Python 2.4.3

I try the following , in this example ls -ltr | grep Aug

#!/usr/bin/python


import commands


Month = "Aug"
status,output = commands.getstatusoutput(" ls -ltr | grep Month "  )
print output

how to insert the Month variable in the command ?

so grep will do that

  | grep Aug

I try this also

status,output = commands.getstatusoutput( " ls -ltr | grep {} ".format(Month) )

but I get the following error

Traceback (most recent call last):
   File "./stamm.py", line 14, in ?
    status,output = commands.getstatusoutput( " ls -ltr | grep {}     ".format(Month) )
AttributeError: 'str' object has no attribute 'format'
9
  • 2
    " ls -ltr | grep {} ".format(Month)? Commented Aug 5, 2015 at 12:19
  • see my update , in the question Commented Aug 5, 2015 at 12:23
  • "'str' object has no attribute 'format'"?! What version of Python are you using? Pre-2.6? You can use " ls -ltr | grep %s " % Month, instead. Commented Aug 5, 2015 at 12:23
  • 2
    @maihabunash you can update that you know Commented Aug 5, 2015 at 12:27
  • 1
    FWIW, parsing the output of ls is generally not recommended, although you can get away with it if you know the file names will always be well-behaved. See BashGuide's Why you shouldn't parse the output of ls and Why not parse ls?. Commented Aug 5, 2015 at 12:41

2 Answers 2

6
import commands


Month = "Aug"
status,output = commands.getstatusoutput(" ls -ltr | grep '" + Month + "'")
print output

Or a couple other possibilites are:

status,output = commands.getstatusoutput("ls -ltr | grep '%s'" % Month)

or

status,output = commands.getstatusoutput(" ls -ltr | grep \"" + Month + "\"")
Sign up to request clarification or add additional context in comments.

2 Comments

I cant use the import subprocess because my python version ( File "/usr/lib/python2.4/subprocess.py", line 550, in init errread, errwrite) )
You could use single quotes on that grep argument, since it's a fixed string. Double quotes in the shell are needed when you want parameter expansion, but that's not needed here. Another possibility is "ls -ltr | grep '%s'" % Month.
0

You don't need to run the shell, there is subprocess module in Python 2.4:

#!/usr/bin/env python
from subprocess import Popen, PIPE

Month = "Aug"
grep = Popen(['grep', Month], stdin=PIPE, stdout=PIPE)
ls = Popen(['ls', '-ltr'], stdout=grep.stdin)
output = grep.communicate()[0]
statuses = [ls.wait(), grep.returncode]

See How do I use subprocess.Popen to connect multiple processes by pipes?

Note: you could implement it in pure Python:

#!/usr/bin/env python
import os
from datetime import datetime

def month(filename):
    return datetime.fromtimestamp(os.path.getmtime(filename)).month

Aug = 8
files = [f for f in os.listdir('.') if month(f) == Aug] 
print(files)

See also, How do you get a directory listing sorted by creation date in python?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.