2

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
 149 iris.txt

i know that i can access shell utilities from python, but i am looking for a python built-in, if there is one.

The crux of my question is getting this information without opening the file (hence my reference to the unix utility *wc -*l)

(is 'sniffing' the correct term for this--i.e., peeking at a file w/o opening it?')

4
  • 5
    wc does open the file. The only information about the content of a file you can obtain without opening it is its length. Commented Mar 24, 2012 at 21:50
  • Good question. Maybe a duplicate of: stackoverflow.com/questions/845058/… I won't vote to close it, though, in case someone comes up with a more novel approach. Commented Mar 24, 2012 at 21:50
  • 1
    @Kevin Reid: completely depends on the file system. Often you can get size, file name, permissions, edit/create date, etc. Commented Mar 24, 2012 at 22:01
  • @bernie--thanks, didn't see that Q--agreed it is close to mine, but isn't directed to getting this info without opening the file, which is really the crux of my question. Commented Mar 24, 2012 at 22:07

2 Answers 2

5

You can always scan through it quickly, right?

lc = sum(1 for l in open('iris.txt'))
Sign up to request clarification or add additional context in comments.

8 Comments

Oh, sorry about missing the 'w/o opening it' part.
No [] needed. And there is no way to do it without opening it. This is a good solution.
This is a nice looking solution but would be better without the square brackets. A generator expression is more memory friendly than a list comprehension.
Actually, I wouldn't mind a brief explanation about this. A generator expression is only valid with (). So I understand why I can change [] to (), but omit it altogether?
@LevLevitsky: look no further than this other SO question: stackoverflow.com/questions/9297653/…
|
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.

1 Comment

That is true about Ethernet packet sniffing. It seems that sniffing is also used in the file-IO context (at least WRT Python's csv module): docs.python.org/library/csv.html#csv.Sniffer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.