python 记录

文件操作

读文件 通过这种方式读取的文件不用主动close

with open('../lib/net/config_environment.dart', 'r+', encoding='utf-8') as file:

读文件全部内容

text = file.read()

定位文件位置

seek(offset,whence)
os.SEEK_SET指针起始位置   0
os.SEEK_CUR指针当前位置   1
os.SEEK_END文件结束位置   2
# 定位到文件开头位置
file.seek(0)

清空文件内容

file.truncate(0)

替换文件中字符串内容

text = text.replace(old, new, 1)

重新写入文件

file.write(text)

字符串是否包含字符串

if 'NetMode.test' in text:
  包含字符串NetMode.test
else:
 不包含字符串NetMode.test

按行读取文件内容

line = file.readline()

移除行结尾的换行符

line.rstrip()

按行写入

file.writelines(line)

导包和安装

把需要的包放入requirements.text中,这样需要直接安装就可以了

requirements.text
requests==2.28.1
GitPython==3.1.29

安装
pip install -r requirements.text


subprocess执行命令行 可等待

import subprocess
process = subprocess.Popen(cmd, shell=True)
process.wait()
returncode = process.returncode
returncode = 0 执行成功
returncode != 0 命令执行失败

Repo git命令

from git import Repo
# 获取git分支
repo = Repo('../')
# 当前分支
branch = repo.active_branch
# repo.iter_commits()
# 拉取远程分支
repo.remote().pull()
posted @ 2023-02-09 11:35  qqcc1388  阅读(27)  评论(0)    收藏  举报