为了账号安全,请及时绑定邮箱和手机立即绑定

正则表达式搜索自动化无聊的东西,功能和错误

正则表达式搜索自动化无聊的东西,功能和错误

慕田峪4524236 2022-08-11 17:19:58
我已经为此挣扎了几个小时:import os, sys, reprint('Type the path to the folder.')input_path = input()input_path = os.path.join(input_path)print('Select the search term')input_term = input()search_regex = re.compile(input_term)all_files = os.listdir(input_path)for j in range(len(all_files)):    new_path = os.path.join(input_path, all_files[j])    search_regex = re.compile(input_term)    target_file = open(new_path, 'r')    file_content = target_file.read()    file_content_in_list = search_regex.findall(file_content)    print('A grand total of ' + str(len(file_content_in_list)) + ' items were found at ' + str(new_path))这按预期工作。该代码读取给定文件夹中的所有文件,并检查找到搜索词的次数。但是,当我尝试将代码的不同部分定义为函数时,我只得到错误:import os, sys, redef select_folder():    print('Type the path to the folder.')    input_path = input()    input_path = os.path.join(input_path)def select_all_files():    all_files = os.listdir(input_path)    for j in range(len(all_files)):        search_a_file()def ask_for_regex():    print('Select the search term')    input_term = input()    search_regex = re.compile(input_term)def search_a_file():    new_path = os.path.join(input_path, all_files[j])    search_regex = re.compile(input_term)    target_file = open(new_path, 'r')    file_content = target_file.read()    file_content_in_list = search_regex.findall(file_content)    print('A grand total of ' + str(len(file_content_in_list)) + ' items were found at ' + str(new_path))select_folder(): #Syntax Error here!    ask_for_regex():        select_all_files():# if i put them like this then everything break down. I guess the variables are forgotten?select_folder()ask_for_regex()select_all_files()我想这个错误是显而易见的,但我无法理解它......
查看完整描述

2 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

正如其他人所提到的,您需要对从函数返回值进行一些读取,然后可以在其他区域使用。我重新格式化了你的代码,以显示它可能是什么样子,看看你是否能挑出所做的更改。我重命名了一些函数,以便更好地描述它们的作用。另外,我没有正式测试这段代码,但它应该让你很好地了解你需要采取什么方向。


另外,仍然试图获得声誉点,所以如果它有帮助,请将答案标记为已接受!


import os

import re



def get_folder_path():

    input_path = input('Type the path to the folder: ')

    return os.path.join(input_path)



def set_regex():

    input_term = input('Type the regex to search for: ')

    return re.compile(input_term)



def search_for_files():

    path = get_folder_path()

    search_regex = set_regex()

    file_list = os.listdir(path)

    count = 0

    for file in file_list:

        with open(file, 'r') as f:

            file_content = f.read()

            match = re.search(search_regex, file_content)

            if match:

                count += 1


    print('A grand total of ' + str(count) + ' items were found at ' + str(path))



search_for_files()


查看完整回答
反对 回复 2022-08-11
?
白衣染霜花

TA贡献1796条经验 获得超10个赞

下面是一个代码示例,类似于您的原始代码,它以线性方式计算直角三角形的斜边:


import math

a = 3

b = 4

c = math.sqrt(a**2 + b**2)

以下是等效的代码,重新编写以使其更易于组合,更加模块化:


import math


def hypotenuse(a, b):

    return math.sqrt(a**2 + b**2)


c = hypotenuse(3, 4)


查看完整回答
反对 回复 2022-08-11
  • 2 回答
  • 0 关注
  • 148 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号