【问题标题】:Check if needed files are in filepaths检查需要的文件是否在文件路径中
【发布时间】:2022-01-20 16:35:07
【问题描述】:

我正在为我的云应用开发一种解决方法,如果不支持 git lfs,它会下载所需的文件。

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

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

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

它总是打印错误,尽管每个文件都已就位。我错过了什么?

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

 # 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/lafllamme/Projects/transcript-app/temp', '/Users/lafllamme/Projects/transcript-app/.DS_Store', ' /Users/laflamme/Projects/transcript-app/requirements.txt', '/Users/laflamme/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/laflamme/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/Projec ts/transcript-app/distilbert-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)

【问题讨论】:

  • foundFiles 中的字符串都不包含在checkFiles 中,就这么简单。
  • 你能写一个普通的for循环(或者,作为提示:两个for循环)来实现你想要的吗?
  • 检查我编辑的帖子
  • 你确定只有当你的检查列表中的所有文件都在这里时它才返回 true 吗?如果只有两个,any([True, False, False, ..., True]) == True => 如果至少有一个文件在这里,那就是True。对于你的问题......我想这很正常,你应该采取相反的方式吗?查看checkfiles 是否在foundfiles 中,否则,您将查找整个路径在checkfiles
  • 没用,找到另一个解决方案!

标签: python path


【解决方案1】:

您应该采取其他方式,查看检查文件是否在找到的文件中。否则,您将查找整个路径:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    相关资源
    最近更新 更多