检查所需文件是否在文件路径中



我正在为我的云应用程序制定一个解决方案,如果不支持git-lfs,它会下载所需的文件。

但是,我需要检查根文件夹中的wandering-sponge-4.pthlabel_embeddings.npy以及distilbert-dlf/pytorch_model.bin下的pytorch_model.bin

我使用Path检查所有目录,并将它们保存为数组中的字符串,这样我就可以在之后检查上面的文件。

我想出了下面的代码,如果所有文件都可用,它会返回true。

它总是打印false,尽管每个文件都在适当的位置。我错过了什么

(我需要检查字符串数组中描述的三个名称(

# 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)

输出:

错误["/Users/lafllamme/Projects/transcript app/labels.csv","/Users/slafllame/Projects/transcripte app/temp","/Users/lafllamme/Products/transcriptive app/.DS_Store","-Users/lafllamme/Protects/transcripte-app/requestions.txt","/Users/lafllamme/Projections/transcript-app/tempDir"fllamme/Projects/transcript app/.gitignore','/Users/lafllamme/Projects/transcripte app/wrandering-sponge-4.pth','/Users/lafllamme/PProjects/transcripte-app/helper.py','-Users/lafllamme/Pprojects/transcript-app/app.py','Users/lafllamme/Projections/transcript-app/packages.txt','.Users/laFL lamme/Products/transcript/app/.git','/Users/lafllamme/Projects/transcripte-app/label_embeddings.npy','/Users/lafllame/Projects/transcript-app/distilbert dlf','/Users/lafllamme/Products/transcript app/distiilbert dlf/config.json','-Users/lafllamme/Protcripte-app/distillbert dlf/pytorch_model.bin']

我想实现这样的目标:

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

解决方案:

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)

您应该使用另一种方法,查看检查文件是否在找到的文件中。否则,您将查找整个路径:

基本上,颠倒你的最后一个条件就足够了:

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

如果至少有一个文件,这将是真的。要知道他们是否都在这里:

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

相关内容

  • 没有找到相关文章

最新更新