Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87673 > unrolled thread

Python script output in file

Started byRobert Clove <cloverobert@gmail.com>
First post2015-03-18 06:35 -0400
Last post2015-03-18 11:10 -0700
Articles 5 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Python script output in file Robert Clove <cloverobert@gmail.com> - 2015-03-18 06:35 -0400
    Re: Python script output in file Rustom Mody <rustompmody@gmail.com> - 2015-03-18 03:49 -0700
      Re: Python script output in file Robert Clove <cloverobert@gmail.com> - 2015-03-18 07:41 -0700
        Re: Python script output in file Rustom Mody <rustompmody@gmail.com> - 2015-03-18 11:06 -0700
          Re: Python script output in file Rustom Mody <rustompmody@gmail.com> - 2015-03-18 11:10 -0700

#87673 — Python script output in file

FromRobert Clove <cloverobert@gmail.com>
Date2015-03-18 06:35 -0400
SubjectPython script output in file
Message-ID<mailman.520.1426674938.21433.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Hi,

I have a perl script named "my_eth-traffic.pl" which calculates the tx and
rx speed of the Ethernet interface in Mb.

I want to run this script from another script and want the output in other
file.
So i wrote the following script but not getting the output.

#!/usr/bin/python

import sys
import subprocess
import datetime
import os
import time

comand8 = "/root/Desktop/my_eth-traffic.pl eth0 M"

filename = logfilename+packetsize+'-'+formated_string+'.txt'
    logfile = open("/root/Desktop/speed.txt", 'w')


proc=subprocess.Popen(comand8,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    for line in proc.stdout:
                   logfile.write(line)

[toc] | [next] | [standalone]


#87674

FromRustom Mody <rustompmody@gmail.com>
Date2015-03-18 03:49 -0700
Message-ID<cb602e99-4158-4eac-8036-095f47041c1b@googlegroups.com>
In reply to#87673
On Wednesday, March 18, 2015 at 4:06:05 PM UTC+5:30, Robert Clove wrote:
> Hi,
> 
> I have a perl script named "my_eth-traffic.pl" which calculates the tx and rx speed of the Ethernet interface in Mb.
> 
> I want to run this script from another script and want the output in other file.
> So i wrote the following script but not getting the output.   
> 
> 
> #!/usr/bin/python
> 
> import sys
> import subprocess
> import datetime
> import os
> import time
> 
> comand8 = "/root/Desktop/my_eth-traffic.pl eth0 M"

From Popen docs 
https://docs.python.org/2/library/subprocess.html#subprocess.Popen


Note in particular that options  and arguments  that are separated by
whitespace in the shell go in separate list elements, while arguments that need 
quoting or backslash escaping when used in the shell (such as filenames...

So (I guess) you should try
comand8 = ["/root/Desktop/my_eth-traffic.pl", "eth0", "M"]
Or better
comand8 = ["perl", "/root/Desktop/my_eth-traffic.pl", "eth0", "M"]
containing spaces or the echo command shown above) are single list elements.

[toc] | [prev] | [next] | [standalone]


#87681

FromRobert Clove <cloverobert@gmail.com>
Date2015-03-18 07:41 -0700
Message-ID<mailman.1.1426689718.10327.python-list@python.org>
In reply to#87674

[Multipart message — attachments visible in raw view] — view raw

./my_eth_script.pl eth0 M >> a.txt

How can i run this command with subprocess.popen

On Wed, Mar 18, 2015 at 3:49 AM, Rustom Mody <rustompmody@gmail.com> wrote:

> On Wednesday, March 18, 2015 at 4:06:05 PM UTC+5:30, Robert Clove wrote:
> > Hi,
> >
> > I have a perl script named "my_eth-traffic.pl" which calculates the tx
> and rx speed of the Ethernet interface in Mb.
> >
> > I want to run this script from another script and want the output in
> other file.
> > So i wrote the following script but not getting the output.
> >
> >
> > #!/usr/bin/python
> >
> > import sys
> > import subprocess
> > import datetime
> > import os
> > import time
> >
> > comand8 = "/root/Desktop/my_eth-traffic.pl eth0 M"
>
> From Popen docs
> https://docs.python.org/2/library/subprocess.html#subprocess.Popen
>
>
> Note in particular that options  and arguments  that are separated by
> whitespace in the shell go in separate list elements, while arguments that
> need
> quoting or backslash escaping when used in the shell (such as filenames...
>
> So (I guess) you should try
> comand8 = ["/root/Desktop/my_eth-traffic.pl", "eth0", "M"]
> Or better
> comand8 = ["perl", "/root/Desktop/my_eth-traffic.pl", "eth0", "M"]
> containing spaces or the echo command shown above) are single list
> elements.
> --
> https://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#87689

FromRustom Mody <rustompmody@gmail.com>
Date2015-03-18 11:06 -0700
Message-ID<ff519dd8-0314-4b0a-a0ec-bd8b961872ea@googlegroups.com>
In reply to#87681
On Wednesday, March 18, 2015 at 8:12:12 PM UTC+5:30, Robert Clove wrote:
> ./my_eth_script.pl eth0 M >> a.txt
> 
> How can i run this command with subprocess.popen

Something like this I guess?

>>> proc = Popen("cat", shell=True, stdout=open(inname, "w"), stdin=open(outname,"r"))

inname and outname should be appropriate input (existing) and output (to be created) files. ie put the strings directly there or assign to the variables
prior to the Popen call

And BTW 
Two suggestions
1. Read the docs
2. Dont top-post https://en.wikipedia.org/wiki/Posting_style#Top-posting

[toc] | [prev] | [next] | [standalone]


#87691

FromRustom Mody <rustompmody@gmail.com>
Date2015-03-18 11:10 -0700
Message-ID<76483040-3ada-4caf-8552-8d99e9e951f5@googlegroups.com>
In reply to#87689
On Wednesday, March 18, 2015 at 11:36:39 PM UTC+5:30, Rustom Mody wrote:
> On Wednesday, March 18, 2015 at 8:12:12 PM UTC+5:30, Robert Clove wrote:
> > ./my_eth_script.pl eth0 M >> a.txt
> > 
> > How can i run this command with subprocess.popen
> 
> Something like this I guess?
> 
> >>> proc = Popen("cat", shell=True, stdout=open(inname, "w"), stdin=open(outname,"r"))

ie
proc = Popen(["perl", "./my_eth_script.pl", "eth0", "M"], shell=True, stdout=open("a.txt", "w"))

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web