How do I kill an empty file ??

Greg Jorgensen gregj at pobox.com
Tue Jan 23 22:33:37 EST 2001


Use the fileinput module:

----
import glob, fileinput
files = glob.glob("path/to/files/*.tab")  # get all file names

for filename in files:
    for line in fileinput.input(filename):
        if len(line) > 2:
            ... # process line
----

fileinput.input gives you an iterator over the lines in the file. It
ignores empty files (opens and closes them immediately), so they won't
be processed by your code.

--
Greg Jorgensen
Portland, Oregon, USA
gregj at pobox.com


In article <94k9p5$vs2$1 at nnrp1.deja.com>,
  chris.nelson at bwc.state.oh.us wrote:
> Hello:
> I have several TAB files that are generated daily for reports that are
> then converted into graphs. The problem is that every now and then a
> report file will fail to generate data and I will have an empty file.
>
> Currently I read in the the file assigning it to a variable and
> readlines()
> Then I use the following to parse out blank lines in a normal file
>
>         for line in data:
> 	    j=0
> 	    if len(line)>2:
>             ......rest of code
>
> What's an easy way to adapt this so that I can ignore files that are
> nothing but empty lines? Ultimately I'm looking for sytax that will
> ignore processing a file if it is empty.
>
> Please advise and tanks,
> Chris
>
> Sent via Deja.com
> http://www.deja.com/
>


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list