Determine if a string appears nowhere in a file

The below code is meant to read lines in an external file and validate them to make sure they can be used as shell commands (more specifically, run a separate executable with command line args). Clearly, my validation can use more work. But, my primary concern is that strategically I don’t really care if the user puts an extra blank line in the config file. What I really want to do is search the whole file and see if there is any single line that contains the string I need. My google fu has not yet yielded any results.

Many thanks in advance

Joe

f = open(configFile, encoding="utf-8")

for i, line in enumerate(f):
    if i == 0:
        if line.find('encrypt_cli') != -1:
            encryptCli = line
        else:
            print("WARNING: No encrypt command defined. Defaulting to " & encryptCli)     
    elif i == 1:
        if line.find('decrypt_cli') != -1:
            encryptCli = line
        else:
            print("WARNING: No decrypt command defined. Defaulting to " & decryptCli)     
    elif i == 2:
        if line.find('decrypt_cli') != -1:
            encryptCli = line
        else:
            print("WARNING: No upload command defined. Defaulting to " & uploadCli)     
    elif i == 3:
        if line.find('decrypt_cli') != -1:
            encryptCli = line
        else:
            print("WARNING: No encrypt command defined. Defaulting to " & downloadCli)     
        break
f.close()

By Theycallmevirgo via Discussions on Python.org at 25Sep2022 00:02:

The below code is meant to read lines in an external file and validate
them to make sure they can be used as shell commands (more
specifically, run a separate executable with command line args).
Clearly, my validation can use more work. But, my primary concern is
that strategically I don’t really care if the user puts an extra blank
line in the config file. What I really want to do is search the whole
file
and see if there is any single line that contains the string I
need. My google fu has not yet yielded any results.

Declare a bunch of flags indicating the initial state, eg:

 saw_encrypt_cli = False

Scan the whole file, setting each flag to True if you see the relevant
string. Check all the flags after the complete file scan.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Hm. That might work. Let me try.

Design a structured file format. Parse that file to a dictionary. If the dictionary is short one or more keys, report it.

For the format, you could use YAML, or design your own; in the latter case, I would recommend (a) having a comment character (eg “from hash to end of line is ignored”), (b) using either “Key: Value” or “Key = Value” format, both of which are well recognized; and (c) having enough flexibility that you can add functionality later.

1 Like

I think in my first version I’ll use Cameron’s solution and in my second version I’ll use yours. I only have the 2 lines to worry about anyhow.

By Theycallmevirgo via Discussions on Python.org at 25Sep2022 23:25:

I think in my first version I’ll use Cameron’s solution and in my
second version I’ll use yours. I only have the 2 lines to worry about
anyhow.

My version is maybe the simplest possible change, designed to get the
required logic flow clear to you. For large files or many strings you
might then pursue more elaborate/efficient/extensible implementations as
desired later.

Cheers,
Cameron Simpson cs@cskk.id.au