title: When running a Perl script from Python and writing the output to a file, why is the file empty?
body: <p>I have the following Perl script that takes an Ethernet interface name and transfer rate as parameters:</p>
  
  <pre><code>#!/usr/bin/perl -w
  
  $prevRXpacks = 0;
  $prevTXpacks = 0;
  $prevRXbytes = 0;
  $prevTXbytes = 0;
  $recentRXpacks = 0;
  $recentTXpacks = 0;
  $recentRXbytes = 0;
  $recentTXbytes = 0;
  $time_wait_default = 2;
  $time_run_default = 0;
  $prev = 0;
  $overflow = 4 * 1024 * 1024 * 1024;
  $k = 1000;
  $m = $k * $k;
  $format = "";
  $format_value = 1;
  $hdr_format = "Bytes/s";
  
  if ( defined ($ARGV[0]) &amp;&amp; ($ARGV[0] eq "--help") ) {
      print "Usage: $0 &lt;device&gt; [-r runtime] [-d delaytime]\n";
      print " -r     time to run traffic monitor in seconds (default = 0 &lt;- forever)\n";
      print " -d     delay between two records in seconds (default = $time_wait_default)\n";
      print " -k, -K    print transfer rate in kbits/s or KBytes/s\n";
      print " -m, -M    print transfer rate in mbits/s or MBytes/s\n";
      die "\n";
  }
  
  open (OUT, "/dev/null");
  select (OUT);
  if ( !defined($ARGV[0]) || !`/sbin/ifconfig $ARGV[0]` ) {
      close (OUT);
      select (STDOUT);
      die "Usage $0 &lt;device&gt; [-r runtime] [-d delaytime] [-kmKM].\nTry --help for more.\n";
  }
  close (OUT);
  select (STDOUT);
  
  $time_wait = $time_wait_default;
  $time_run = $time_run_default;
  
  $i = 1;
  
  while ( defined ($ARGV[$i]) ) {
      if ($ARGV[$i] eq "-d") {
          $time_wait = $ARGV[++$i] || $time_wait_default;
      }
      if ($ARGV[$i] eq "-r") {
          $time_run = $ARGV[++$i] || $time_run_default;
      }
      if ($ARGV[$i] eq "-K") {
          $format = $hdr_format = "KBytes/s";
          $format_value = $k;
      }
      if ($ARGV[$i] eq "-k") {
          $format = $hdr_format = "Kbits/s";
          $format_value = $k/8;
      }
      if ($ARGV[$i] eq "-M")  {
          $format = $hdr_format = "MBytes/s";
          $format_value = $m;
      }
      if ($ARGV[$i] eq "-m") {
          $format = $hdr_format = "Mbits/s";
          $format_value = $m/8;
      }
  
      $i++;
  }
  
  print "Traffic monitoring...\n";
  
  # main loop
  $time_loop = $time_run + time();
  
  while ( time() &lt; $time_loop || !$time_run ) {
      $recent = time();
      foreach $_ (`/sbin/ifconfig $ARGV[0]`) {
          if (/RX.*packets/) {
              ($blabla1, $blabla2, $blabla3) = split(/\s+/);
              ($blabla1, $recentRXpacks, $blabla2) = split(/:/, $blabla3);
          }
          if (/TX.*packets/) {
              ($blabla1, $blabla2, $blabla3) = split(/\s+/);
              ($blabla1, $recentTXpacks, $blabla2) = split(/:/, $blabla3);
          }
          if (/RX.*bytes/) {
              ($blabla1, $blabla2, $blabla3) = split(/:/);
              ($recentRXbytes) = split(/ /, $blabla2);
              ($recentTXbytes) = split(/ /, $blabla3);
          }
      }
  
      if ( ($recent &gt; $prev) &amp;&amp; $prev) {
          $interval = $recent - $prev;
  #       $interval = $time_wait;
  
          $RXpacks = round (rate ($prevRXpacks, $recentRXpacks, $interval));
          $TXpacks = round (rate ($prevTXpacks, $recentTXpacks, $interval));
          $RXbytes = round (rate ($prevRXbytes, $recentRXbytes, $interval)/$format_value);
          $TXbytes = round (rate ($prevTXbytes, $recentTXbytes, $interval)/$format_value);
  
          write;
      }
  
      $prev = $recent;
      $prevRXpacks = $recentRXpacks;
      $prevTXpacks = $recentTXpacks;
      $prevRXbytes = $recentRXbytes;
      $prevTXbytes = $recentTXbytes;
  
      $time_delay = $time_wait + time();
      while (time() &lt; $time_delay) {}
  }
  
  format STDOUT_TOP =
  RX [packets/s]    TX [packets/s]    RX [@&lt;&lt;&lt;&lt;&lt;&lt;&lt;]        TX [@&lt;&lt;&lt;&lt;&lt;&lt;&lt;]
                     $hdr_format,           $hdr_format
  .
  
  format STDOUT =
  @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;    @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;    @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;    @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
  $RXpacks,    $TXpacks,    $RXbytes." ".$format,    $TXbytes." ".$format
  .
  
  sub rate {
      my ($prev, $recent);
      ($prev, $recent, $interval) = @_;
      if (($recent = $recent - $prev) &lt;0) {
          $recent += $overflow;
      }
      return $recent/$interval;
  }
  
  sub round {
      my ($mod, $result);
      $result = int ($_[0]);
      $mod = ($_[0] - $result) ;
      if ( $mod &gt;= 0.5) {
          $result++;
      }
      return $result;
  }
  </code></pre>
  
  <p>I tried to run the above script from Python and write the output to a file using the following code:</p>
  
  <pre><code>#!/usr/bin/python
  
  import sys
  import subprocess
  import datetime
  import os
  import time
  
  command = ["perl", "/root/Desktop/myethtraffic.pl", "eth0", "M"]
  
  dir_name = "/root/Desktop/"
  
  logfile = open("/root/Desktop/aa1a.txt", 'w')
  
  proc = subprocess.Popen(command)
  for line in proc.stderr:
          logfile.write(line)
  </code></pre>
  
  <p>The file is created but is empty. How can I fix this?</p>