Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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<ullat>-?[\d.]+).*?
    ULLON:\ (?P<ullon>-?[\d.]+).*?
    LRLAT:\ (?P<lrlat>-?[\d.]+)""", re.DOTALL|re.VERBOSE)

I have the data in .txt file as a sequence:

QUADNAME: rockport_colony_SD
RESOLUTION: 10 ULLAT: 43.625 ULLON:
-97.87527466 LRLAT: 43.5 LRLON: -97.75027466 HDATUM: 27 ZMIN: 361.58401489 ZMAX: 413.38400269 ZMEAN: 396.1293335 ZSIGMA: 12.36359215 PMETHOD: 5 QUADDATE: 20001001

How can I use the Python -file to process the .txt -file?

I guess that we need a parameter in the .py file, so that we can use a syntax like in terminal:

$ py-file file-to-be-processed

This question was raised by the post here.

like image 207
Léo Léopold Hertz 준영 Avatar asked Jan 29 '09 10:01

Léo Léopold Hertz 준영


People also ask

How do you pass a filename as a parameter in Python?

This can be done by passing a comma-separated list of file names as one of the arguments while running the script. FOr example, if you have a script called `myscipt.py' you would run it as: python myscript.py file1,file2,file3.

How do you enter another file in Python?

inputFileName = input("Enter name of input file: ") inputFile = open(inputFileName, "r") print("Opening file", inputFileName, " for reading.") 1. Open the file and associate the file with a file variable (file is “locked” for writing). 2.


1 Answers

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.

like image 195
Andrew Wilkinson Avatar answered Oct 08 '22 14:10

Andrew Wilkinson