admin 管理员组文章数量: 1086925
python 读取文件内容,及遍历list列表
python3
读取的yaml文件内容格式
all:- .git- .git"内容":- "你好"- "世界"
脚本内容
#!/usr/bin/python3
# -*- coding: utf-8 -*-import os
import yaml# 获取当前路径
current_path = os.getcwd()
print("current_path:", current_path)git_username= "XXXXXX"
git_password= "XXXXXX"
# 读取文件内容,仅读取内容采用1处,要是文件同时也是yaml文件采取2处
def get_yaml_file (yaml_file):# 加上 encoding='utf-8',处理配置文件中含中文出现乱码的情况。with open(yaml_file, 'r', encoding='utf-8') as yaml_data:print(type(yaml_data))print(yaml_data)# 1,仅打印文本内容str_data = yaml_data.read()yaml_data.close()print(type(str_data))print(f"打印出read()方法后的信息:\n{str_data}")# 2,调用yaml.load()方法;5.1弃用了load原本的用法,加上Loader=yaml.FullLoader,避免报错提示不安全yaml_cont = yaml.load(str_data, Loader=yaml.FullLoader)print(type(yaml_cont))print("打印出yaml.load()方法后信息:\n", yaml_cont)list_data = yaml_cont.get("all")print(list_data)return list_data# 遍历列表里面的git 地址内容,并克隆git clone
def ergodic_list(list_data):for data in list_data:protocol = data.split("//")[-2].strip()print(protocol)git_url = data.split("//")[1].strip()print(git_url)git_clone_cmd =f"git clone {protocol}//'{git_username}':'{git_password}'@{git_url}"print(git_clone_cmd)os.system(git_clone_cmd)
if __name__ == '__main__':list_data = get_yaml_file ( f"{current_path}\git_list.yaml" )# 遍历克隆代码ergodic_list(list_data)
打印到控制台的信息
本文标签: python 读取文件内容,及遍历list列表
版权声明:本文标题:python 读取文件内容,及遍历list列表 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1699365280a345143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论