Skip to content Skip to sidebar Skip to footer

Is There A Built-in Python Analog To Unix 'wc' For Sniffing A File?

Everyone's done this--from the shell, you need some details about a text file (more than just ls -l gives you), in particular, that file's line count, so: @ > wc -l iris.txt 14

Solution 1:

You can always scan through it quickly, right?

lc = sum(1 for l in open('iris.txt'))

Solution 2:

No, I would not call this "sniffing". Sniffing typically refers to looking at data as it passes through, like Ethernet packet capture.

You cannot get the number of lines from a file without opening it. This is because the number of lines in the file is actually the number of newline characters ("\n" on linux) in the file, which you have to read after open()ing it.

Post a Comment for "Is There A Built-in Python Analog To Unix 'wc' For Sniffing A File?"