123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051# @Author: xdd2026@qq.com# @CreateData: 2023-10-25 15:50:40# @MaintenanceDate: 2023-10-25 15:50:40# @Purpose: python 提交本地文件到 githubimport subprocessimport datetimeimport time# 定义要切换到的目录repo_directory = r"M:\blog_hexo"# 切换到指定目录try: # 使用subprocess运行cd命令切换目录 subprocess.run(f"cd /d {repo_directory} && git status", shell=True, check=True, stdout=subprocess.PIPE)except subprocess.CalledProcessError as e: print("切换目录时出现错误:", e) exit(1)# 获取当前时间字符串current_datetime = datetime.datetime.now()formatted_datetime = current_datetime.strftime("%Y-%m-%d_%H%M%S")# 写 log文件with open(f"{repo_directory}/log.txt", "w") as fw: fw.write(formatted_datetime + '\n')# 执行Git命令(例如:git add, git commit, git push)try: # 拉取语雀文章 subprocess.run(f"cd /d {repo_directory} && yuque-hexo sync", shell=True, check=True) # 添加文件 subprocess.run(f"cd /d {repo_directory} && git add .", shell=True, check=True) # 提交更改 subprocess.run(f"cd /d {repo_directory} && git commit -m '{formatted_datetime}'", shell=True, check=True) # 推送到GitHub subprocess.run(f"cd /d {repo_directory} && git push", shell=True, check=True) print("成功提交到GitHub")except subprocess.CalledProcessError as e: print("=" * 80) print("执行Git命令时出现错误:", e) exit(1)