<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Script removing files smaller than 10kb - script development problem. in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/script-removing-files-smaller-than-10kb-script/m-p/1195387#M65061</link>
    <description>&lt;P&gt;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.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Path = arcpy.GetParameterAsText(0)
if any([Path != '', not Path]):
    Path = r'c:/.../default value if ran as script tool'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 25 Jul 2022 13:14:38 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2022-07-25T13:14:38Z</dc:date>
    <item>
      <title>Script removing files smaller than 10kb - script development problem.</title>
      <link>https://community.esri.com/t5/python-questions/script-removing-files-smaller-than-10kb-script/m-p/1193348#M65010</link>
      <description>&lt;P&gt;Hi.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;But I cant get it to work, and I'm not good at python or any type of programming...&lt;/P&gt;&lt;P&gt;This is the code I have been trying to get to work:&lt;/P&gt;&lt;P&gt;import os, os.path&lt;/P&gt;&lt;P&gt;for root, _, files in os.walk("C:/some/dir"):&lt;BR /&gt;for f in files:&lt;BR /&gt;fullpath = os.path.join(root, f)&lt;BR /&gt;try:&lt;BR /&gt;if os.path.getsize(fullpath) &amp;lt; 10 * 1024: #set file size in kb&lt;BR /&gt;print fullpath&lt;BR /&gt;os.remove(fullpath)&lt;BR /&gt;except WindowsError:&lt;BR /&gt;print "Error" + fullpath&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2022 13:08:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-removing-files-smaller-than-10kb-script/m-p/1193348#M65010</guid>
      <dc:creator>JohnnyJohansson</dc:creator>
      <dc:date>2022-07-19T13:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: Script removing files smaller than 10kb - script development problem.</title>
      <link>https://community.esri.com/t5/python-questions/script-removing-files-smaller-than-10kb-script/m-p/1193372#M65012</link>
      <description>&lt;P&gt;What errors are you getting when you run it?&amp;nbsp; Including that in your post would help troubleshoot it.&lt;/P&gt;&lt;P&gt;If you are using 2.7:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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 &amp;lt; 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'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you are using 3, wrap the print statements in ().&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2022 13:31:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-removing-files-smaller-than-10kb-script/m-p/1193372#M65012</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-07-19T13:31:46Z</dc:date>
    </item>
    <item>
      <title>Re: Script removing files smaller than 10kb - script development problem.</title>
      <link>https://community.esri.com/t5/python-questions/script-removing-files-smaller-than-10kb-script/m-p/1195351#M65059</link>
      <description>&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the working code with inputes for script tool for Arcgis.&lt;/P&gt;&lt;P&gt;I havent tested the error code function sinse it just worked..&lt;/P&gt;&lt;LI-CODE lang="c"&gt;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 &amp;lt; 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'
            &lt;/LI-CODE&gt;&lt;P&gt;i&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 11:23:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-removing-files-smaller-than-10kb-script/m-p/1195351#M65059</guid>
      <dc:creator>JohnnyJohansson</dc:creator>
      <dc:date>2022-07-25T11:23:20Z</dc:date>
    </item>
    <item>
      <title>Re: Script removing files smaller than 10kb - script development problem.</title>
      <link>https://community.esri.com/t5/python-questions/script-removing-files-smaller-than-10kb-script/m-p/1195387#M65061</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Path = arcpy.GetParameterAsText(0)
if any([Path != '', not Path]):
    Path = r'c:/.../default value if ran as script tool'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 13:14:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-removing-files-smaller-than-10kb-script/m-p/1195387#M65061</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-07-25T13:14:38Z</dc:date>
    </item>
  </channel>
</rss>

