3 examples of 'length of file python' in Python

Every line of 'length of file python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
225def file_len(fname):
226 try:
227 with open(fname) as f:
228 for i, l in enumerate(f):
229 pass
230 return i + 1
231 except:
232 return 0
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

11def file_len(fname):
12 i = 0
13 with open(fname) as f:
14 for i, l in enumerate(f):
15 pass
16 return i + 1
27def file_len(fname):
28 """Count the number of lines in a file."""
29 with open(fname) as f:
30 for i, l in enumerate(f):
31 pass
32 return i + 1

Related snippets