Home Backend Development Python Tutorial Use python script to count the number of lines in a file

Use python script to count the number of lines in a file

Oct 20, 2016 am 10:10 AM
python script

Python can count the number of lines in a file, do you believe it? Whether you believe it or not, I do. Let's take a look at how python counts the number of lines in a file. The code is very simple. I also made comments and it is very simple to implement. . .

1

2

3

4

5

6

7

8

9

#_*_coding:utf_8
#引入sys和系统os模块
import sys 
import os 
'''
计算某一个文件的行数
'''
def countFileLines(filename): 
    count = 0
    try: 
        handle = open(filename, 'r') 
        #循环文件句柄
for eachline in handle:
            #累加
            count += 1
    #异常捕获
    except IOError, e: 
        print 'file open error', e
    return count 
    
print countFileLines('/home/pythontab/tmp/a.php')
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Python script to create random jokes using pyjokes Python script to create random jokes using pyjokes Sep 13, 2023 pm 08:25 PM

Do you want to add some humor to your Python script or application? Whether you're building a chatbot, developing a command line tool, or just want to entertain yourself with random jokes, the pyjokes library can help. With pyjokes you can easily generate jokes in various categories and customize them to your liking. In this blog post, we will explore how to create random jokes in Python using the pyjokes library. We'll cover the installation process, generating different categories of jokes, customizing jokes, displaying them in a console application or web page, and handling any potential errors that may occur. Install pyjokes Before we start using pyjokes to create random jokes, we need

Do you know some reasons why crontab scheduled tasks are not executed? Do you know some reasons why crontab scheduled tasks are not executed? Mar 09, 2024 am 09:49 AM

Summary of some reasons why crontab scheduled tasks are not executed. Update time: January 9, 2019 09:34:57 Author: Hope on the field. This article mainly summarizes and introduces to you some reasons why crontab scheduled tasks are not executed. For everyone Solutions are given for each of the possible triggers, which have certain reference and learning value for colleagues who encounter this problem. Students in need can follow the editor to learn together. Preface: I have encountered some problems at work recently. The crontab scheduled task was not executed. Later, when I searched on the Internet, I found that the Internet mainly mentioned these five incentives: 1. The crond service is not started. Crontab is not a function of the Linux kernel, but relies on a cron.

Exploring Orange3: Opening up a new world of data mining and machine learning! Exploring Orange3: Opening up a new world of data mining and machine learning! Mar 04, 2024 pm 08:16 PM

Orange3 is a powerful open source data visualization and machine learning tool. It has rich data processing, analysis and modeling functions, providing users with simple and fast data mining and machine learning solutions. This article will briefly introduce the basic functions and usage of Orange3, and combine it with actual application scenarios and Python code cases to help readers better master the usage skills of Orange3. The basic functions of Orange3 include data loading, data preprocessing, feature selection, model establishment and evaluation, etc. Users can use the intuitive interface to drag and drop components to easily build data processes. At the same time, more complex data processing and modeling tasks can also be completed through Python scripts. Below we will go through a practical

Python script automatically refreshes Excel spreadsheet Python script automatically refreshes Excel spreadsheet Sep 09, 2023 pm 06:21 PM

Python and Excel are two powerful tools that when combined can open up a world of automation. Python has versatile libraries and user-friendly syntax that enable us to write scripts to perform various tasks efficiently. Excel, on the other hand, is a widely used spreadsheet program that provides a familiar interface for data analysis and manipulation. In this tutorial, we will explore how to leverage Python to automate the process of refreshing Excel spreadsheets, saving us time and effort. Do you find yourself spending valuable time manually refreshing your Excel spreadsheet with updated data? This is a repetitive and time-consuming task that can really kill productivity. In this article we will guide you through using Py

PyCharm Advanced Tutorial: Use PyInstaller to package code into EXE format PyCharm Advanced Tutorial: Use PyInstaller to package code into EXE format Feb 20, 2024 am 09:34 AM

PyCharm is a powerful Python integrated development environment that provides a wealth of functions and tools to help developers improve efficiency. Among them, PyInstaller is a commonly used tool that can package Python code into an executable file (EXE format) to facilitate running on machines without a Python environment. In this article, we will introduce how to use PyInstaller in PyCharm to package Python code into EXE format, and provide specific

Python script for monitoring website changes Python script for monitoring website changes Aug 29, 2023 pm 12:25 PM

In today's digital age, being aware of the latest changes on your website is crucial for a variety of purposes, such as tracking updates on your competitors' websites, monitoring product availability, or staying informed of important information. Manually checking your website for changes can be time-consuming and inefficient. This is where automation comes into play. In this blog post, we will explore how to create a Python script to monitor website changes. By leveraging the power of Python and some handy libraries, we can automate the process of retrieving website content, comparing it to previous versions, and notifying us of any changes. This allows us to remain proactive and react promptly to updates or modifications to the sites we monitor. Setting up the environment Before we start writing scripts to monitor website changes, we need to set up P

How to read excel data in pycharm How to read excel data in pycharm Apr 03, 2024 pm 08:42 PM

How to read Excel data using PyCharm? The steps are as follows: install the openpyxl library; import the openpyxl library; load the Excel workbook; access a specific worksheet; access cells in the worksheet; traverse rows and columns.

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

See all articles