1
def read_files(file_allmail,file_blacklist):

    with open("all_email_large.txt", 'r') as f:
        allmail = f.read().splitlines()

    with open("bkacklist_large.txt", 'r') as f1:
        blacklist = f1.read().splitlines()

    return allmail,blacklist

Instead of pre-selecting a document I would like to be able to parameterize them in the command line. In other words, I want to be able to select different documents that python should make a list of.

chepner
  • 497,756
  • 71
  • 530
  • 681
lazer
  • 25
  • 2
  • 2
    argparse is a great tool to do that :) https://docs.python.org/3/library/argparse.html – Anna Semjén Mar 11 '20 at 16:27
  • 3
    Does this answer your question? [How to read/process command line arguments?](https://stackoverflow.com/questions/1009860/how-to-read-process-command-line-arguments) – jwalton Mar 11 '20 at 16:28
  • 1
    Your function already appears to take (though ignore) file names as arguments. I assume the hard-coding would actually take place when you *call* the function? `read_files("all_email_large.txt", "blacklist_large.txt")` – chepner Mar 11 '20 at 16:29

2 Answers2

2

You probably want argparse: https://docs.python.org/3/howto/argparse.html

Changed slightly, but from the docs:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("file_allmail", help="doc to open")
parser.add_argument("file_blacklist", help="doc to open")
args = parser.parse_args()

def read_files(file_allmail, file_blacklist):
   with open(file_allmail, 'r') as f:
       allmail = f.read().splitlines()

    with open(file_blacklist, 'r') as f1:
        blacklist = f1.read().splitlines()

    return allmail, blacklist

if __name__ == '__main__':
    allmail, blacklist = read_files(args.file_allmail, args.file_blacklist)
    print(allmail)
    print(blacklist)

You call it with:

python your_python.py --file_allmail all_email_large.txt --file_blacklist bkacklist_large.txt
Ollie in PGH
  • 2,559
  • 2
  • 16
  • 19
1

If you alter your function, then you can simply call read_files('filename1.txt','filename2.txt') at any point for any text files.

def read_files(file_allmail,file_blacklist):

    with open(file_allmail, 'r') as f:
        allmail = f.read().splitlines()

    with open(file_blacklist, 'r') as f1:
        blacklist = f1.read().splitlines()

    return allmail,blacklist
katardin
  • 596
  • 3
  • 14