Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: read output script twice and write two columns to csv
    primarykey
    data
    text
    <p>This is my first python <strong>(2.4)</strong> program and I could use your help. I have a csv file</p> <pre><code>HOST, PATH server1, /path/to/file1.py server2, /path/to/file2.py server3, /path/to/file3.py </code></pre> <p>That executes a command to each PATH on column 2/row[1]</p> <p>In the output of the command I search for text and append them to new columns.</p> <p>Example command output:</p> <pre><code>Command: python /path/to/file1.py Output: server1 (NTFS) Reply:Yes </code></pre> <p>I would like make my new csv file like:</p> <pre><code>HOST, PATH, PLATFORM, REPLY server1, /path/to/file1.py, Windows, Yes server2, /path/to/file2.py, Linux, Yes server3, /path/to/file3.py, BSD, No </code></pre> <p>I havnt yet got it to work with reading the same output line and appending two columns with different results. I have tried closing the writer and opening a new one within the same reader with no joy. I have tried indenting with no luck. The closest I have gotten is having it accurately search for both strings but it only writes to one column.</p> <p>I have also tried first searching for the os platform and writing the changes to a new file Then, opening the new file to write another row</p> <p>Of course I could force this to work by running the commands again and searching separately, but thats redundant and unnecessary.</p> <pre><code>import datetime import csv import os, time from stat import * # ST_SIZE etc from subprocess import Popen, PIPE, STDOUT # Set Date now = datetime.datetime.now() today = now.strftime("%m-%d-%Y") # Files filename = "my_list.csv" results = "results/results_" + today + ".csv" # Eg. results_04-14-2012.csv # Commands command = "python" SP = " " incsv = open(filename, 'rb') try: reader = csv.reader(incsv) outcsv = open(results, 'w') try: writer = csv.writer(outcsv) for row in reader: p = Popen(command + SP + row[1], shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) stdout, empty = p.communicate() print 'Command: %s\nOutput: %s\n' % (command + SP + row[1], stdout) if not stdout: writer.writerow(row + ['PLATFORM']) # Create header for new column when first line is empty on stdout elif 'BSD' in stdout: writer.writerow(row + ['BSD']) elif 'Linux' in stdout or 'swap' in stdout or 'LLVM' in stdout or 'EBR' in stdout: writer.writerow(row + ['Linux']) elif 'NTFS' in stdout: writer.writerow(row + ['Windows']) else: writer.writerow(row + ['Error Scanning']) reply = open(results, 'w') try: writer = csv.writer(platform) for row in reader: if not stdout: writer.writerow(row + ['REPLY']) # Create header for new column when first line is empty on stdout elif 'Reply:Yes' in stdout: writer.writerow(row + ['Yes']) elif 'Reply:No' in stdout : writer.writerow(row + ['No']) else: writer.writerow(row + ['Error']) finally: reply.close() finally: outcsv.close() finally: incsv.close() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload