我正在Python上为copy cron config编写脚本。我需要将文件复制到/etc/cron.d/
,如果目标文件不存在,则必须创建它。我找到了解决方案,但它没有提供丢失的文件,它是:
from shutil import copyfile
def index():
src = "/opt/stat/stat_cron"
dst = "/etc/cron.d/stat_cron"
copyfile(src, dst)
if __name__ == "__main__":
index()
我得到异常"FileNotFoundError: [Errno 2] No such file or directory: '/etc/cron.d/stat_cron'"
请告诉我正确的解决方法。你知道吗
谢谢大家。成功解决了下一个问题:
使用pathlib检查文件是否存在,如果不存在,则创建一个文件。你知道吗
如果文件存在,使用
os.makedirs
可以帮助检查条件,如果不存在,则创建一个相关问题 更多 >
编程相关推荐