一些实用的Python脚本(长期更新)

主要靠抄,东拼西凑。

拼接文字批量输出文本

prestr = "前面的"
lastStr = "后面的\n"
#\n表示回车
name = "";
#列表顶部标题
for num in range(0,100):
#这里产生0-99
        n1 = prestr+str(num)+ lastStr
        name = name+n1;    
fo = open("Test.txt","w+")
#命名
fo.write(name)

自动重启外壳

import subprocess, time, sys

TIME = 60
CMD = "spider.py"


class Auto_Run():
    def __init__(self, sleep_time, cmd):
        if sys.version_info < (3, 6):
            print("only support python 3.6 and later version")
            sys.exit(1111)
        self.sleep_time = sleep_time
        self.cmd = cmd
        self.ext = (cmd[-3:]).lower()
        self.p = None
        self.run()

        try:
            while 1:
                time.sleep(sleep_time)
                self.poll = self.p.poll()
                if self.p.poll() is None:
                    print("restarting......")
                    self.p.kill()
                    self.run()

                else:
                    print("starting......")
                    self.run()
        except KeyboardInterrupt as e:
            print("exit???")

    def run(self):
        if self.ext == ".py":
            print('Started!')
            # use now running python version, think multiple python installed and now use python3.6 to run
            python_path = sys.executable
            print("use the absolute path of python to run", python_path)
            self.p = subprocess.Popen([python_path, '%s' % self.cmd], stdin=sys.stdin, stdout=sys.stdout,
                                      stderr=sys.stderr, shell=False)
        else:
            pass


app = Auto_Run(TIME, CMD)

评论区

点踩数过多的评论会被枪毙

已有 1 条评论