内容中心

2026年热门的隔断拆除专业公司推荐-青岛泰鑫伟建筑劳务有限公司

青岛泰鑫伟建筑劳务有限公司,是一家专门从事建筑物及构筑物拆除、桥梁切割、混凝土切割、桥梁机械切割拆除、静力拆除、地基基础处理、结构改造、机械设备租凭、各种静力爆破拆除工程; 危旧房屋改造加固及使用功能改变的结构加固补强、植筋锚固、抗震减震、新老砼界面处理、裂缝灌浆、普通粘钢、粘碳纤维、化学锚固、化学锚栓和加大截面加固、砖混结构改造及建筑防水防腐工程; 同时专注别墅加固、别墅扩建、别墅翻新、别墅防水、地下室挖建设计施工、游泳池挖建防水、钢结构设计制作安装、新建钢结构、钢结构增层、钢结构拆除、钢结构楼梯设计制作安装等建筑加固、建筑结构新建及建筑结构改造工程的专业施工队伍。能够独立完成各种大型工业与民用建筑、道路、桥梁、厂房、车站等拆除加固工程的企业 我公司拥有各种拆除机械设备,在多年的拆除施工中,我公司逐渐磨练出了一支以工程技术人员、施工管理人员以及业务骨干为核心,训练有素、作风顽强、善打硬仗、享誉同行业的施工队伍。他们拥有精湛的专业技术、丰富的施工经验,在许多工期短、难度大的拆除项目中,精心设计、大胆创新,创造出了建筑拆除业的多项施工纪录。

To solve this problem, we need to create a Python script that searches for a specific string in a given file, reports the line numbers where the string appears, counts occurrences per line, and provides a total count of occurrences. The script should handle common errors (like file not found or permission issues) and offer an option for case-sensitive or case-insensitive search.

Approach

  1. Input Handling:

    • Prompt the user for the file path.
    • Prompt for the search string (ensure it's not empty).
    • Ask if the search should be case-sensitive.
  2. File Processing:

    • Open the file in read mode (with error handling for common issues like missing files or permission errors).
    • Read the file line by line to avoid memory issues with large files.
    • For each line, check if the search string exists (considering case sensitivity).
    • Count occurrences per line and total occurrences.
  3. Output:

    • Print lines where the string is found (with line numbers and occurrence count per line).
    • Print the total number of occurrences or a message if no matches are found.

Solution Code

def search_file():
    # Get file path from user
    file_path = input("Enter the path to the file you want to search: ").strip()

    # Get non-empty search string
    while True:
        search_str = input("Enter the string to search for: ").strip()
        if search_str:
            break
        print("Error: Search string cannot be empty. Please try again.")

    # Case sensitivity option
    case_sensitive = input("Perform case-sensitive search? (y/n): ").strip().lower() == 'y'

    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            line_number = 0
            total_occurrences = 0

            for line in file:
                line_number += 1
                # Adjust for case sensitivity
                if case_sensitive:
                    line_check = line
                    str_check = search_str
                else:
                    line_check = line.lower()
                    str_check = search_str.lower()

                # Check for matches
                if str_check in line_check:
                    count_in_line = line_check.count(str_check)
                    total_occurrences += count_in_line
                    # Print the original line (without trailing newline)
                    print(f"\nLine {line_number}: {line.rstrip()}")
                    print(f"Occurrences in this line: {count_in_line}")

            # Final report
            if total_occurrences == 0:
                print(f"\nNo occurrences of '{search_str}' found in the file.")
            else:
                print(f"\nTotal occurrences of '{search_str}': {total_occurrences}")

    except FileNotFoundError:
        print(f"\nError: File '{file_path}' not found.")
    except PermissionError:
        print(f"\nError: You do not have permission to read '{file_path}'.")
    except UnicodeDecodeError:
        print(f"\nError: The file '{file_path}' is not a text file (binary or unsupported encoding).")
    except Exception as e:
        print(f"\nUnexpected error: {str(e)}")

if __name__ == "__main__":
    search_file()

Explanation

  • Input Validation: The script ensures the search string is not empty to avoid invalid searches.
  • Case Sensitivity: Users can choose between case-sensitive and case-insensitive searches, making the tool flexible.
  • Error Handling: The script catches common errors (file not found, permission issues, non-text files) and provides meaningful messages to the user.
  • Efficiency: Reading the file line by line ensures that even large files are processed without excessive memory usage.
  • Clear Output: The script prints the exact lines where the string is found, along with line numbers and occurrence counts, making it easy to locate matches.

This script is user-friendly and robust, suitable for both basic and slightly advanced search needs in text files.

青岛泰鑫伟建筑劳务有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?