Skip to content

Instantly share code, notes, and snippets.

@elundmark
Created September 19, 2011 16:43
Show Gist options
  • Save elundmark/1226923 to your computer and use it in GitHub Desktop.
Save elundmark/1226923 to your computer and use it in GitHub Desktop.
Count lines in text-file (works w/ Nautilus Script)
#!/usr/bin/python -tt
import sys
import os
import subprocess
def main(filenames):
for filename in filenames:
fcmd = "file -bi '"+filename+"'"
f = os.popen(fcmd,"r")
if f.read().startswith("text"):
with open(filename) as f:
for i, l in enumerate(f):
pass
i = i + 1
i = str(i)
subprocess.call(("notify-send -t 7000 -i 'format-text-bold-symbolic' 'Python Line Counter' '"+filename+" has "+i+" lines.'"), shell=True)
else:
subprocess.call(("notify-send -t 7000 -i error 'Python Line Counter' '"+filename+" isn not a textfile!!'"), shell=True)
sys.exit(1)
if __name__ == "__main__":
args = sys.argv[1:]
if len(args) == 0:
subprocess.call(("notify-send -t 3000 'Python Line Counter' 'Error: Call this as a Nautilus Script, or \'Count_the_lines.py myfile.txt\''"), shell=True)
sys.exit(1)
else:
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment