Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7787039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:32:29+00:00 2026-06-01T20:32:29+00:00

Python 2.4 For my example I have a 2 column csv file Eg: HOST,

  • 0

Python 2.4
For my example I have a 2 column csv file

Eg:

HOST, FILE
server1, /path/to/file1
server2, /path/to/file2
server3, /path/to/file3

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:

 HOST, PATH, FILESIZE
 server1, /path/to/file1, 6546542
 server2, /path/to/file2, 46546343
 server3, /path/to/file3, 87523

Ive tried a couple methods but havnt had much success.

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

 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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:32:30+00:00Added an answer on June 1, 2026 at 8:32 pm

    Sketch w/o error handling:

    #!/usr/bin/env python
    
    import csv
    import os
    
    filename = "sample.csv"
    # localhost, 01.html.bak
    # localhost, 01.htmlbak
    # ...
    
    def filesize(filename):
        # no need to shell out for filesize
        return os.stat(filename).st_size
    
    with open(filename, 'rb') as handle:
        reader = csv.reader(handle)
        # result is written to sample.csv.updated.csv
        writer = csv.writer(open('%s.updated.csv' % filename, 'w'))
        for row in reader:
            # need to strip filename, just in case
            writer.writerow(row + [ filesize(row[1].strip()) ])
    
    # result
    # localhost, 01.html.bak,10021
    # localhost, 01.htmlbak,218982
    # ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CSV file with 5 columns. Using Python, how can I delete
I have a CSV (comma separated values) file that contains student information. The column
I have lots of dates in a column in a CSV file that I
Is it possible to have a list be evaluated lazily in Python? For example
Possible Duplicate: Python, compute list difference I have two lists For example: A =
In python, you can concatenate boolean values, and it would return an integer. Example:
I have a CSV file with the following format: ID | STUFF | Custom
I have the following example in Python to print a table: job_IDs = ['13453',
I have a python file, i pass the log file and a url to
In a file I am reformatting, I would like to put the last column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.