gpt4 book ai didi

python 检查更新脚本

转载 作者:太空宇宙 更新时间:2023-11-03 21:16:26 34 4
gpt4 key购买 nike

正如标题所说,我希望我的一些 python 脚本具有“检查更新”功能。我正在阅读,有一种方法可以使用程序版本创建 .txt 文件,但我不想创建 .txt 文件,我认为还有另一种方法可以做到这一点。我想用来自服务器的python文件读取内容并与实际内容进行比较,但同时我认为这是一个坏主意。

我想创建一个属性“self.version = 1.0”,但是有一种方法可以从服务器读取python文件的属性?这样比较两者就更容易了。

因为创建文本文件的问题是,如果我将.py发送给 friend ,我也必须发送txt文件,如果我有10个.py和10个txt文件,并且如果我更新版本我必须在 txt 文件中执行 .py,我认为这不是很直观。

你们都推荐我什么?

谢谢!

最佳答案

脚本之间版本检查的一种方法可能是这样的:

将模块变量放在脚本顶部,例如:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__VERSION__ = '1.5'

# here goes the rest of the script

然后,检查更新(这可能位于脚本开头的某个位置):

  1. 从服务器下载最新版本的脚本(也许使用requests库并保存到磁盘上的某个位置或临时文件中)。
  2. 将脚本作为文本文件读取以获取其版本

    new_version = ''
    with open('new_script.py') as f:
    for line in f:
    line = line.strip()
    if '__VERSION__' in line:
    _, new_version = line.split('=', maxsplit=1)
    new_version = new_version.strip()
    break

    print('Version of the new script is:', new_version)
    print('Current version is:', __VERSION__)

关于python 检查更新脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54672414/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com