Call a system command inside Python and get its output result, not the exit status

In Python, I want to count the number of lines in a file xh-2.txt.

import subprocess subprocess.call("wc -l xh-2.txt",shell=True)

But this is giving me exit status, not the result of the command.

I know the command print os.popen("wc -l xh-2.txt|cut -d' ' -f1").read() will do the job, but popen is depreciated and why use read()?

What is the best way to call a system command inside Python and get its output result, not exit status?

--------------Solutions-------------

Use subprocess.check_output().

Run command with arguments and return its output as a byte string.

>>> import subprocess
>>> import shlex
>>> cmd = 'wc -l test.txt'
>>> cm = shlex.split(cmd)
>>> subprocess.check_output(cm,shell=True)
' 1 test.txt\n'
>>>

You could use subprocess recipe

from subprocess import Popen, PIPE
Popen("wc -l xh-2.txt", shell=True, stdout=PIPE).communicate()[0]

Category:python Time:2012-02-12 Views:1
Tags: python

Related post

  • Running shell command from python and capturing the output 2011-01-21

    I want to write a function that will execute a shell command and return it's output as a string, no matter, is it an error or success message. I just want to get the same result that I would have gotten with the command line. Can anybody give a code

  • How to depends of a system command with python/distutils? 2010-08-12

    I'm looking for the most elegant way to notify users of my library that they need a specific unix command to ensure that it will works... When is the bet time for my lib to raise an error: Installation ? When my app call the command ? At the import o

  • Executing system commands in python 2011-08-19

    I have experience with perl for writing scripts, which made it easy for me executing linux commands by using back-ticks. I was wondering, how can I do this Python ? Is there a special way for capturing the result of a command (output) ? thank you :)

  • evaluate system command or python script in firefox browser using javascript 2011-10-07

    May be duplicate. I would like to write javascript which can execute linux command in firefox (seems to be impossible but asking with hope) from googling, found that it is possible for IE through "ActiveXObject". here is sample code: <SCRIPT type=

  • How to execute a VS2008 command from Python and grab its output? 2010-02-13

    I wish to run tf changeset 12345 Using the Visual Studio 2008 Command tool. It is located in: "c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\" and the command that gets launched is: %comspec% /k ""c:\Program Files (x86)\Microsoft Visual Studi

  • In Ubuntu Python 2.7 reading output of an os.system() command 2011-08-20

    So I am running code that looks like this on Ubuntu 11.4 and Python 2.7: p_scan_command = "sudo nmap -sC -sV -PN -O 192.168.0.1/24" time.sleep(1.5) os.system(p_scan_command) f = open('nmapscan1.log', 'r') print f.read() f.close() What is happening is

  • Unable to view a file using win32 dir command and Python subprocess 2012-04-09

    I'm attempting to read in a file and produce a hash of said file. For the most part, this has gone smoothly. However, there are a few files that I am unable to get a handle to. For instance: file("c:\\windows\\system32\\nvvsvc.exe") Produces the erro

  • what is the system function in python 2010-07-06

    I want to play with system command in python . for example we have this function in perl : system("ls -la"); and its run ls -la what is the system function in python ? Thanks in Advance . --------------Solutions------------- It is os.system: import o

  • Can we execute operating system command from sqlite3 2011-05-22

    My requirement is: To run a shell script when a new entry is added to a table. Is there any way to add a trigger to run the shell script on inserting new row? Well, the situation is: I am using the db of a different program. Say Program X creates and

  • C++ - system command 2011-11-08

    I have a C++ program which is mainly used for video processing. Inside the program, I am launching the system command in order to obtain pass the processed videos to some other binaries to postprocess them. I have a while loop towards infinite and I

  • Running a linux command from python 2012-03-16

    I need to run this linux command from python and assign the output to a variable. ps -ef | grep rtptransmit | grep -v grep I've tried using pythons commands library to do this. import commands a = commands.getoutput('ps -ef | grep rtptransmit | grep

  • In Python - how to execute system command with no output 2009-02-01

    Is there a built-in method in Python to execute a system command without displaying the output? I only want to grab the return value. It is important that it be cross-platform, so just redirecting the output to /dev/null won't work on Windows, and th

  • Run a linux system command as a superuser, using a python script 2009-02-24

    I have got postfix installed on my machine and I am updating virtual_alias on the fly programmatically(using python)(on some action). Once I update the entry in the /etc/postfix/virtual_alias, I am running the command: sudo /usr/sbin/postmap /etc/pos

  • why this dos command does not work inside python? 2010-01-20

    I try to move some dos command from my batch file into python but get this error, The filename, directory name, or volume label syntax is incorrect, for the following statement. subprocess.Popen('rd /s /q .\ProcessControlSimulator\bin', shell=True, s

  • Python run system command and then exit- won't exit 2011-07-24

    I have the following code: os.system("C:/Python27/python.exe C:/GUI/TestGUI.py") sys.exit(0) It runs the command fine, and a window pops up. However, it doesn't exit the first script. It just stays there, and I eventualy have to force quit it. No err

  • variable inside a string used as an os command in python 2014-03-05

    I need to execute a system command in which there is a variable.In the code below os.system('SMILExtract -C config/demo/demo1_energy.conf -I user.wav -O csv/user.csv') in the string "user.wav", user is a variable string and .wav is the constant strin

  • How to escape os.system() calls in Python? 2008-08-30

    When using os.system() it's often necessary to escape filenames and other arguments passed as parameters to commands. How can I do this? Preferably something that would work on multiple operating systems/shells but in particular for bash. I'm current

  • How do I run "dot" as a command from Python? 2009-06-05

    I am using Python on Mac OSX Leopard. I am trying to run the program 'dot' (part of Graphviz) from Python: # -*- coding: utf-8 -*- import os print os.environ['PATH'] print os.system("ls >> a.txt") print os.system("dot -o9.png -Tpng ./6.dot") Th

  • Calling an external command in Python 2008-09-18

    How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script? --------------Solutions------------- Look at the subprocess module in the stdlib: from subprocess import call call(["ls",

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.381 (s). 13 q(s)