LaFllamme
LaFllamme

Reputation: 288

Check if needed files are in filepaths

I am working on a workaround for my cloud-app, that downloads the needed files, if git lfs is not supported.

However, I need to check for wandering-sponge-4.pth, label_embeddings.npy in the root folder and pytorch_model.bin under distilbert-dlf/pytorch_model.bin

I used Path to check all directories and save them as a string inside an array, so I can check for the above files afterwards.

I came up with the code down below, that returns me true, if all files are available.

It always prints false, although every file is in place. What did I miss?

(I need to check the described three names in my array of strings)

 # ugly workaround because streamlit cloud doesn't support git lfs -.-
for model in Path().cwd().glob("./*"):
    foundFiles.append(str(model))
for files in Path().cwd().glob("distilbert-dlf/*"):
    foundFiles.append(str(files))

checkFiles = ["pytorch_model.bin", "wandering-sponge-4.pth", "label_embeddings.npy"]

output = any([substring in checkFiles for substring in foundFiles])
print(output, foundFiles)

Output:

False ['/Users/lafllamme/Projects/transcript-app/labels.csv', '/Users/lafllamme/Projects/transcript-app/temp', '/Users/lafllamme/Projects/transcript-app/.DS_Store', '/Users/lafllamme/Projects/transcript-app/requirements.txt', '/Users/lafllamme/Projects/transcript-app/tempDir', '/Users/lafllamme/Projects/transcript-app/pycache', '/Users/lafllamme/Projects/transcript-app/env', '/Users/lafllamme/Projects/transcript-app/.gitignore', '/Users/lafllamme/Projects/transcript-app/wandering-sponge-4.pth', '/Users/lafllamme/Projects/transcript-app/helper.py', '/Users/lafllamme/Projects/transcript-app/.gitattributes', '/Users/lafllamme/Projects/transcript-app/app.py', '/Users/lafllamme/Projects/transcript-app/packages.txt', '/Users/lafllamme/Projects/transcript-app/.git', '/Users/lafllamme/Projects/transcript-app/label_embeddings.npy', '/Users/lafllamme/Projects/transcript-app/distilbert-dlf', '/Users/lafllamme/Projects/transcript-app/distilbert-dlf/config.json', '/Users/lafllamme/Projects/transcript-app/distilbert-dlf/pytorch_model.bin']

I want to achieve something like this:

if (file1, file2 not in directory and file 3 not in subdirectory):
   ....

Solution:

for model in Path().cwd().glob("./*"):
    foundFiles.append(str(model))
for files in Path().cwd().glob("distilbert-dlf/*"):
    foundFiles.append(str(files))

checkFiles = ("distilbert-dlf/pytorch_model.bin", "wandering-sponge-4.pth", "label_embeddings.npy")
for path in checkFiles:
    if os.path.exists(path)==True:
        print('I found:', path)

Upvotes: 0

Views: 58

Answers (1)

Metapod
Metapod

Reputation: 416

You should do the other way, looking if the check files are in the found files. Otherwise, you look for the whole path:

Basically, inverting your last condition should be enough:

output = any([[checkFile in substring for substring in foundFiles] for checkFile in checkFiles])

This will be true if there is AT LEAST one of the files. To know if they are all here:

output = all([any([checkFile in substring for substring in foundFiles]) for checkFile in checkFiles])

Upvotes: 1

Related Questions