Python脚本检查目录中的文件,然后给我发电子邮件的名称

2024-04-26 00:38:50 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,正如标题所示,我是Python新手,我的第一个项目是一个脚本,它将检查5个目录中是否存在特定的文件。如果存在,我希望它给我发电子邮件,它在每个路径中找到的文件名。你知道吗

这是我目前掌握的情况。。你知道吗

    from pathlib import *


adr01_file = Path("/home/skeer/Documents/Projects/Python_temp/home/adr01/upload/monitor*.pgp")
if adr01_file.is_file():
    print("File exists")
else:
    print("File does not exist")


chpt_file = Path("/home/skeer/Documents/Projects/Python_temp/home/chpt/upload/monitor*.pgp")
if adr01_file.is_file():
    print("File exists")
else:
    print("File does not exist")


emb01_file = Path("/home/skeer/Documents/Projects/Python_temp/home/emb01/upload/monitor*.pgp")
if adr01_file.is_file():
    print("File exists")
else:
    print("File does not exist")


exp01_file = Path("/home/skeer/Documents/Projects/Python_temp/home/exp01/upload/monitor*.pgp")
if adr01_file.is_file():
    print("File exists")
else:
    print("File does not exist")


iix01_file = Path("/home/skeer/Documents/Projects/Python_temp/home/iix01/upload/monitor*.pgp")
if adr01_file.is_file():
    print('File exists')
else:
    print('File does not exist')

但我得到的是每一行的“文件存在”。即使在任何目录中除了adr01之外没有任何文件。你知道吗

感谢所有提示/提示。你知道吗


Tags: pathhomeifistempdocumentsmonitorfile
1条回答
网友
1楼 · 发布于 2024-04-26 00:38:50

这是因为您检查了五次adr01_file的存在性—您忘记了更改if语句中的变量。另外,我在你的路径中看到了*,你的意思是字面意思还是通配符?如果再晚一点,就不行了。你知道吗

相关问题 更多 >

    热门问题