Select to view content in your preferred language

Script removing files smaller than 10kb - script development problem.

391
3
Jump to solution
07-19-2022 06:03 AM
JohnnyJohansson
New Contributor III

Hi.

I have some LAS files that contain nothing, but my sub program dosent like empty files, so I'm trying to write an script that removes all files smaller that ex. 10kb.

But I cant get it to work, and I'm not good at python or any type of programming...

This is the code I have been trying to get to work:

import os, os.path

for root, _, files in os.walk("C:/some/dir"):
for f in files:
fullpath = os.path.join(root, f)
try:
if os.path.getsize(fullpath) < 10 * 1024: #set file size in kb
print fullpath
os.remove(fullpath)
except WindowsError:
print "Error" + fullpath

0 Kudos
1 Solution

Accepted Solutions
JohnnyJohansson
New Contributor III

Thanks!

 

This is the working code with inputes for script tool for Arcgis.

I havent tested the error code function sinse it just worked..

import arcpy, os, os.path

Path = arcpy.GetParameterAsText(0)
Size = arcpy.GetParameterAsText(1)
CalSeize = ( int(Size) * 1024)
arcpy.AddMessage ("Path to search for files: " +(Path))
arcpy.AddMessage ("Min file seize to keep: " + (Size)+"Kb")
arcpy.AddMessage ("Calculatde min size: " + str(CalSeize) + " bytes")
for root, _, files in os.walk((Path)):
    for f in files:
        fullpath = os.path.join(root, f)
        fileSize = os.path.getsize(fullpath)
        if fileSize < CalSeize: # Calculate min file value to keep
            try:
                os.remove(fullpath)
                arcpy.AddMessage ("Removed: " + f + " with the size: " +  str(fileSize) + " bytes" )
                print 'removed: ' + f + ' file size: ' + str(fileSize) + 'bytes'
            except WindowsError as ex:
                print "Error: " + fullpath
        else:
            print fullpath + f + ' is within min. File size: ' + str(fileSize) + 'bytes'
            

i

View solution in original post

0 Kudos
3 Replies
by Anonymous User
Not applicable

What errors are you getting when you run it?  Including that in your post would help troubleshoot it.

If you are using 2.7:

for root, _, files in os.walk(r"C:\..."):
    for f in files:
        fullpath = os.path.join(root, f)
        fileSize = os.path.getsize(fullpath)
        if fileSize < 10240:  # no need to do the calculation becuase it will never change so just use the value.
            try:
                os.remove(fullpath)
                print 'removed: ' + f + ' file size: ' + str(fileSize) + 'kb'
            except WindowsError as ex:
                print "Error: " + fullpath
        else:
            print fullpath + f + ' is within min. File size: ' + str(fileSize) + 'kb'

 

If you are using 3, wrap the print statements in ().

0 Kudos
JohnnyJohansson
New Contributor III

Thanks!

 

This is the working code with inputes for script tool for Arcgis.

I havent tested the error code function sinse it just worked..

import arcpy, os, os.path

Path = arcpy.GetParameterAsText(0)
Size = arcpy.GetParameterAsText(1)
CalSeize = ( int(Size) * 1024)
arcpy.AddMessage ("Path to search for files: " +(Path))
arcpy.AddMessage ("Min file seize to keep: " + (Size)+"Kb")
arcpy.AddMessage ("Calculatde min size: " + str(CalSeize) + " bytes")
for root, _, files in os.walk((Path)):
    for f in files:
        fullpath = os.path.join(root, f)
        fileSize = os.path.getsize(fullpath)
        if fileSize < CalSeize: # Calculate min file value to keep
            try:
                os.remove(fullpath)
                arcpy.AddMessage ("Removed: " + f + " with the size: " +  str(fileSize) + " bytes" )
                print 'removed: ' + f + ' file size: ' + str(fileSize) + 'bytes'
            except WindowsError as ex:
                print "Error: " + fullpath
        else:
            print fullpath + f + ' is within min. File size: ' + str(fileSize) + 'bytes'
            

i

0 Kudos
by Anonymous User
Not applicable

Good work. Just a tip, you can set a conditional on the Parameter assignment so you can run it as a standalone script or as the script tool so you don't have to maintain two scripts.

Path = arcpy.GetParameterAsText(0)
if any([Path != '', not Path]):
    Path = r'c:/.../default value if ran as script tool'