user3151668
user3151668

Reputation: 3

python 3.3 user input

I need to create a Python 3.3 program that searches a file for a specific string and prints the line if the string is found. I have code that works but I am forced to rewrite the program each time I want to run it.

import re
fh = open('C:\Web_logs\ex130801.txt')
for line in fh:
    if "admin_" in line:
        print(line)

Is there a way to accept user input for the file’s path, i.e. C:\Web_logs\ex130801.txt?

Upvotes: 0

Views: 3351

Answers (3)

jgfооt
jgfооt

Reputation: 954

You can use the argparse module, new in 3.2.

First, import the library at the top of your file:

import argparse

Then, create a parser object, and tell it that you're looking for two command-line strings, the first one the filename and the second one the string you are searching for:

parser = argparse.ArgumentParser(description='searches a file for a specific string and prints the line if the string is found.')
parser.add_argument("filename", type=str,help="file to search through")
parser.add_argument("searchstring", type=str, help="string to look for")

Then, run the parser object on the command line, getting back an object that contains the fields you are looking for, as strings:

args = parser.parse_args()

Now, "args.filename" contains your filename and "args.searchstring" contains the search string, so rewrite your loop this way:

fh = open(args.filename)
for line in fh:
    if args.searchstring in line:
        print(line)

From the command line, one of your users can now just do:

$ python3 searcher.py /usr/dict/words bana

The best part is, if you user fails to give the arguments you are looking for, the script nicely tells them the syntax you are looking for:

$ python3 searcher.py
usage: searcher.py [-h] filename searchstring
searcher.py: error: the following arguments are required: filename, searchstring

Even better, the user can enter the --help option to get documentation for your program:

python3 .\searcher.py --help
usage: searcher.py [-h] filename searchstring

searches a file for a specific string and prints the line if the string is found.

positional arguments:
  filename      file to search through
  searchstring  string to look for

optional arguments:
  -h, --help    show this help message and exit

Don't forget that you can also add #!/usr/bin/python3 to the top of your code and change the executable flag and then it isn't necessary to type python3 on the command line.

Upvotes: 1

henrebotha
henrebotha

Reputation: 1298

import re
path = input("Please enter path to file: ")
fh = open(path)
for line in fh:
    if "admin_" in line:
        print(line)

Remember to close your file when you're done! A more Pythonic way, assuming you don't need to do something else to the file:

import re
path = input("Please enter path to file: ")
with open(path) as fh:
    for line in fh:
        if "admin_" in line:
            print(line)

The with statement closes the file object once you leave that block.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1124828

Sure, you can use the sys.argv list to read a filename from the command line:

import sys

with open(sys.argv[1]) as fh:
    for line in fh:
        if "admin_" in line:
            print(line)

You need to invoke your script with the filename on the command line:

python scriptname.py C:\Web_logs\ex130801.txt

Another option is to use the input() function to ask the user to enter a filename when the script is running:

filename = input('Please enter a filename: ')    
with open(filename) as fh:
    for line in fh:
        if "admin_" in line:
            print(line)

Upvotes: 4

Related Questions