Note that there are some explanatory texts on larger screens.

plurals
  1. POget file size and append to new column of CSV file
    text
    copied!<p>Python 2.4 For my example I have a 2 column csv file</p> <p>Eg:</p> <pre><code>HOST, FILE server1, /path/to/file1 server2, /path/to/file2 server3, /path/to/file3 </code></pre> <p>I would like to get the file size of the object at PATH for each row in the csv FILE, then add that value to the csv FILE on a new column. Making it:</p> <pre><code> HOST, PATH, FILESIZE server1, /path/to/file1, 6546542 server2, /path/to/file2, 46546343 server3, /path/to/file3, 87523 </code></pre> <p>Ive tried a couple methods but havnt had much success.</p> <p>The code below executes fileSizeCmd (du -b) on the PATH and outputs the filezie correctly, but I havnt figured out how to use the data to add to the csv FILE</p> <pre><code> import datetime import csv import os, time from subprocess import Popen, PIPE, STDOUT now = datetime.datetime.now() fileSizeCmd = "du -b" SP = " " # Try to get disk size and append to another row after entry above #st = os.stat(row[3]) #except IOError: #print "failed to get information about", file #else: #print "file size:", st[ST_SIZE] #print "file modified:", time.asctime(time.localtime(st[ST_MTIME])) incsv = open('my_list.csv', 'rb') try: reader = csv.reader(incsv) outcsv = open('results/results_' + now.strftime("%m-%d-%Y") + '.csv', 'wb') try: writer = csv.writer(outcsv) for row in reader: p = Popen(fileSizeCmd + SP + row[1], shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) stdout, empty = p.communicate() print 'Command: %s\nOutput: %s\n' % (fileSizeCmd + SP + row[1], stdout) # Results in bytes example # # Output: # 8589935104 /path/to/file # # Write 8589935104 to new column of csv FILE finally: outcsv.close() finally: incsv.close() </code></pre>
 

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