Make a file searching program that accepts a single regular expression with sys.argv.

Question:

I need to make a program that does exactly what the title states. I need my program to search through all the current files and all sub directories to see if any files match the pattern. If any files match the pattern, the whole pathname for the file should be printed on the screen.

I have some of this completed, however some parts may not make sense since I copied and pasted some parts from examples given to me in class:

#!/usr/bin/env python

import re, glob, sys, os
if (len(sys.argv) < 2):
        print("You need an argument.")
        sys.exit(0)

var = sys.argv(1)

#re.search(var,

data = os.walk(".")
for tup in data:
        dirname = tup[0]
        dirlist = tup[1]
        dirlist = tup[2]

        for file in filelist:
                fullname = os.path.join(dirname, file)
                username = owner(fullname)
                if re.data:
                print(os.path.abspath(os.path.join(username, fullname)))

Basically, when I run the program in Linux I need to type the name of the program and then an argument right afterwards. That argument is the name of the file I want to search for. So if I typed “./lab7.py .txt” I should expect the program to return any .txt files. I am still a beginner and the answer may seem pretty obvious to some of you, but I just can’t seem to get that final step needed to make this program run the way it should. Any help is appreciated, thank you.

Asked By: DoTNeT

||

Answers:

Your .txt argument is not meant to be a regular expression but a simple extension, so you don’t need the regular expression package.

Here’s my version. Since it can be an extension but also a part of the filename, just use in. I’ve also simplified the owner() part which made no sense (apart from that your code was close)

import os,sys

if (len(sys.argv) < 2):
        print("You need an argument.")
        sys.exit(0)

var = sys.argv[1]

for root,dirs,files in os.walk("."):
    for f in files:
        if var in f:
            fullname = os.path.join(root, f)
            print(os.path.abspath(fullname))

If extension matches, the full filepath is printed. Also works in sub-directories.

This answer shows a highlevel sketch of a module cli based approach to create utilities which are reusable.

For example my_find . | my_grep 'txt'.

Python setup.py develop vs install

mkdir -p Homework/my_homework
touch Homework/setup.py
touch Homework/my_homework/assignment1.py

virtualenv venv_homework
source venv_homework/bin/activate
python setup.py develop
my_find . | my_grep 'txt'

setup.py:

from setuptools import setup, find_packages


setup(name='my_homework',
      install_requires=[
          # e.g. 'gevent>=0.0.0',
      ],
      entry_points={
          "console_scripts": [
              "my_find=my_homework.assignment1:list_directories",
              "my_grep=my_homework.assignment1:search_a_string",
          ]
      },
      include_package_data=True,
      packages=find_packages(),
      package_data={
           # config files
      },
      zip_safe=False)

Homework/my_homework/assignment1.py:

import fileinput
import argparse
import os, sys

def list_directories():
    # https://stackoverflow.com/questions/2212643/python-recursive-folder-read
   sys.stdout.write('/some/directory/with/a/file.txtn')
   sys.stdout.write('/some/directory/without/a/filen') 


def search_a_string():
    '''
        reads from stdout and gets a regex from from sys.argv via argparse
        returns any found regexes back to stdout     
    '''
    # extra credit rewrite ArgumentParser using sys.argv
    argparser = argparse.ArgumentParser()
    argparser.add_argument('my_grep_args', metavar='MY_GREP_ARGS', nargs='*')
    print(my_grep_args)
    for line in fileinput.input():
        # https://stackoverflow.com/questions/19300020/python-match-a-string-with-regex
        sys.stdout.write('regex found {}n'.format(line))

There is some magic but, basically the setup.py via setuptools or python setup.py develop will place the functions my_find and my_grep into $PATH similar to calling something like python -m 'my_homework.assignment1.list_directories' . | python -m 'my_homework.assignment1.search_a_string' 'txt'

Related:

Answered By: jmunsch
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.