Skip to content Skip to sidebar Skip to footer

How Can I Pass A Filename As A Parameter Into My Module?

I have the following code in .py file: import re regex = re.compile( r'''ULLAT:\ (?P-?[\d.]+).*? ULLON:\ (?P-?[\d.]+).*? LRLAT:\ (?P

Solution 1:

You need to read the file in and then search the contents using the regular expression. The sys module contains a list, argv, which contains all the command line parameters. We pull out the second one (the first is the file name used to run the script), open the file, and then read in the contents.

import re
import sys

file_name = sys.argv[1]
fp = open(file_name)
contents = fp.read()

regex = re.compile(
    r"""ULLAT:\ (?P-?[\d.]+).*?
    ULLON:\ (?P-?[\d.]+).*?
    LRLAT:\ (?P-?[\d.]+)""", re.DOTALL|re.VERBOSE)

match = regex.search(contents)

See the Python regular expression documentation for details on what you can do with the match object. See this part of the documentation for why we need search rather than match when scanning the file.

This code will allow you to use the syntax you specified in your question.

Post a Comment for "How Can I Pass A Filename As A Parameter Into My Module?"