Skip to main content
Solved

compare file sizes using python

  • Feb 7, 2019 4:15 PM UTC
  • 3 replies
  • 160 views

Forum|alt.badge.img

I need to compare file sizes and other attributes for a PRE upgrade and POST upgrade output files. O would like to get the list of files from both folders and compare the sizes, names and type. I am new to python and really need help writing a python script.

Best answer by david_r

If you're not comfortable using Python, my recommendation would be to simply use the "Directory and File Pathnames" reader, it can give you the file sizes straight away:

 

That way you can avoid using Python.

View original
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

3 replies

david_r
Celebrity
  • Feb 7, 2019 4:25 PM UTC

Try something like:

import os

def compare_file_sizes(feature):
    filesize1 = os.stat(feature.getAttribute('filename1')).st_size
    filesize2 = os.stat(feature.getAttribute('filename2')).st_size
    feature.setAttribute('filename1_size', filesize1)
    feature.setAttribute('filename2_size', filesize2)

Given an input feature with the attributes "filename1" and "filename2", it will add the two attributes "filename1_size" and "filename2_size" to the output feature.

You can e.g. combine this with the "Directory and File Pathnames" reader to list the contents of any directory.


Forum|alt.badge.img
  • Author
  • Feb 7, 2019 4:40 PM UTC

@david_r thanks for your reply, do I replace 'filename1' and 'filename2' with the file path and name? I need better understanding on how to apply this code.

Thanks


david_r
Celebrity
  • Best Answer
  • Feb 7, 2019 4:47 PM UTC

If you're not comfortable using Python, my recommendation would be to simply use the "Directory and File Pathnames" reader, it can give you the file sizes straight away:

 

That way you can avoid using Python.