是否有一个内置的python类似于unix'wc'用于嗅探文件?
问题描述:
每个人都这样做了 - 从shell开始,你需要一些关于文本文件的详细信息(不仅仅是ls -l给你),特别是那个文件的 行计算 ,所以:
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
我知道我可以访问shell来自python的实用程序,但我正在寻找内置的python,如果有的话。
i know that i can access shell utilities from python, but i am looking for a python built-in, if there is one.
我的问题的关键是获取此信息 没有打开文件 (因此我引用了unix实用程序* wc - * l)
The crux of my question is getting this information without opening the file (hence my reference to the unix utility *wc -*l)
(正在'嗅探'正确的术语 - 即,在没有打开它的情况下偷看文件?')
答
你可以随时快速扫描一下,对吗?
You can always scan through it quickly, right?
lc = sum(1 for l in open('iris.txt'))