c++ - 在 Linux 中搜索用户定义的文件

标签 c++ python linux bash

我的任务是用 python(或 c++ 或 bash)创建一个搜索程序,我在编码方面确实没有那么丰富的经验,所以如果这根本不在正确的轨道上,抱歉!

用户将需要输入关键字 I.E“Report”,该函数将需要在所有目录中查找名为“report”的文件。我在这里编写的这个脚本似乎可以运行到第 5 行。有人可以帮我吗?谢谢!

import os

keyword = raw_input ("What would you like to search for?")

os.chdir("/home/noob")

for files in os.listdir("."):

  if files(keyword):

     print files

最佳答案

我猜您遇到了 TypeError: 'list' object is not callable。那是因为 os.listdir 返回文件列表。

您需要执行以下操作之一:

if keyword in files: # for an exact match

或者

for filename in files:
    if keyword.lower() in filename.lower(): #for case insensitive searching

关于c++ - 在 Linux 中搜索用户定义的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19381932/

相关文章:

C++将字符串的元素分配给新字符串

c++ - 如何从进程内部确定 CPU 和内存消耗

python - tkinter 列表框用 python 拖放

python - 强制 Jupyter Notebook *不* 打开网络浏览器

c++ - 从 X11 请求图像,用 C 或 C++ 合成 WM

python - 跟踪自上次击键以来的时间?

linux - 删除 symfony 项目

linux - 使用 PhoneGap Build 在 Linux 上进行 PhoneGap/Cordova 开发的最低限度设置

c - 宏替换

c++ - 将 Qt::[Enum/Flag] 注册为 QtMetaType