首页 \ 问答 \ 从不同文件中搜索关键字并返回文件名(Search keyword from different files and return the filename)

从不同文件中搜索关键字并返回文件名(Search keyword from different files and return the filename)

我是使用python的新手,我尝试过每一个关键词并尝试搜索以帮助我弄清楚我的问题。 我想创建一个程序,帮助我从文件夹中的不同txt文件中搜索关键字并返回文件名(python)。

这是我搜索的成果:

import glob
import os

path = r'<filepath of the folder>'
keyword = "internet" //ex is internet

for filename in glob.glob(os.path.join(path, '*.txt')):

    f = open(filename)
    if keyword in f:
    print("filename")

我试过跑步(令人惊讶的是)它运行正常,但没有打印但是我很确定有一个带有互联网词的文件。 因为它没有打印任何错误或任何东西,不确定我是否正在朝着正确的方向。


I'm super duper new in using python and I've tried every keywords and attempts on searching to help me figure out my problem. I want to create a program that helps me search a keyword from different txt files in a folder and return the filename (python).

This has been the fruit of my searches:

import glob
import os

path = r'<filepath of the folder>'
keyword = "internet" //ex is internet

for filename in glob.glob(os.path.join(path, '*.txt')):

    f = open(filename)
    if keyword in f:
    print("filename")

I tried running and (surprisingly) it run properly but there's nothing printed but I'm quite sure that there's a file with an internet word inside. And since it didn't print any error or anything at all, not sure if I'm even in the right direction.


原文:https://stackoverflow.com/questions/36276273
更新时间:2022-06-14 15:06

最满意答案

http://api.jquery.com/fadeout/你同时淡出和淡出可能看起来很奇怪。 尝试:

<div id="frame_content">

    <?php
    if (empty($_GET['content']) or ! $_GET['content']) {
        $file = 'fotografie_1.inc.php';
    } else {
        $file = $_GET['content'] . ".inc.php";
    }
    if (file_exists($file)) {
        include($file);
    }
    ?> 

</div>

<script>
    function getthings(param1, param2) {
        $.ajax({
            url: "fotografie_1.inc.php",
            type: "GET",
            data: {chapter: param1, content: param2},
            async: true
        }).done(function (data) {
            $("#frame_content").fadeOut("slow", function () { //Callback for when the fadeout is done
                $("#frame_content").empty();
                $("#frame_content").append(data);
                $("#frame_content").fadeIn("slow");
            });
        });
    }
    $(document).ready(function () {
        getthings(1, 'fotographie_1'); // or whatever default is sensible
    });
</script>

http://api.jquery.com/fadeout/ You're fading in and out simultaneously which may look odd. Try:

<div id="frame_content">

    <?php
    if (empty($_GET['content']) or ! $_GET['content']) {
        $file = 'fotografie_1.inc.php';
    } else {
        $file = $_GET['content'] . ".inc.php";
    }
    if (file_exists($file)) {
        include($file);
    }
    ?> 

</div>

<script>
    function getthings(param1, param2) {
        $.ajax({
            url: "fotografie_1.inc.php",
            type: "GET",
            data: {chapter: param1, content: param2},
            async: true
        }).done(function (data) {
            $("#frame_content").fadeOut("slow", function () { //Callback for when the fadeout is done
                $("#frame_content").empty();
                $("#frame_content").append(data);
                $("#frame_content").fadeIn("slow");
            });
        });
    }
    $(document).ready(function () {
        getthings(1, 'fotographie_1'); // or whatever default is sensible
    });
</script>

相关问答

更多
  • 我建议hide/show标题而不是通过fadeIn/fadeOut动画,请检查以下代码: onChange:function(opt,idx){ if(caption[idx]) { $("#desc").html(caption[idx]); } else { $("#desc").html(""); } }, 其背后的原因是内部幻灯片正在运行一些计时器事件,一旦您尝试调用函数fadeIn/fadeOut其他计时器事件就会被绑定,并且它们不会与原始 ...
  • 我能想到的唯一可能的原因是元素最初并未隐藏 - 要使fadeIn()工作,必须首先隐藏元素。 所以要么使用脚本隐藏它 function test(jQuery) { $("#fade").hide().delay(500).fadeIn(4000); $("#fade2").hide().delay(500).fadeIn(4000); //typo //this can be shorten to //$("#fade, #fade2").hide().delay(500) ...
  • 使用jQuery的.queue $("#Target") .fadeOut() .queue(function() { if (id != "") // Do Something $(this).load( "/Site/Controller/Action/"+id, null, $(this).dequeue ); else ...
  • http://api.jquery.com/fadeout/你同时淡出和淡出可能看起来很奇怪。 尝试:
  • 您的数组值没有目录条目,因此子项从错误的目录加载。 更改数组条目以包括完整路径,或者包含使用HTML5数据属性读取的图像的基本目录属性。 我已经包含了后者的一个工作示例 - 尝试尽可能保持原始脚本完整。 不断重新查找选择器是不好的做法,因此我做了一个小优化并将$('#picture')选择器存储在变量中。 从技术上讲,你应该尝试将自己限制在方法中的一个var声明中,尽管它会像你拥有它一样工作。 $(document).ready(function() { var imageName=["Dai ...
  • 我无法正确测试它,因为我没有完整的代码(以及图像)。 但这应该可以解决问题: $(function () { $("#gallery div").attr("active", false); $("#thumb-1").click(function () { $("#first-div").attr("active", true).fadeIn(); }); $("#forward-button").click(function () { ...
  • 你只需要在改变路径和淡入淡出之前淡出图像...所以你的点击函数应该变成: 编辑 :忘记显示/隐藏动画异步发生,所以你需要使用淡出回调来触发剩下的...代码应该是: