小编nma*_*ues的帖子

python - 初学者 - 在程序中集成optparse

我已经开始认真学习Python作为我的第一门编程语言,并掌握一些算法基础知识.由于每个人都建议最好的方法是找到有用的东西,我决定用一个小脚本来管理我的存储库.

基本事项: - 启用/禁用YUM存储库 - 更改当前YUM存储库的优先级 - 添加/删除存储库

虽然解析文件并替换/添加/删除数据非常简单,但我正在努力(主要是因为可能缺乏知识)与'optparse'的单一事物......我想添加一个选项(-l)列出了当前可用的存储库...我已经创建了一个简单的函数来完成这项工作(不是非常详细的),但是我无法将它与optparse上的'-l'连接起来.任何人都可以提供如何做到这一点的例子/建议?

当前脚本是这样的:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import optparse
import ConfigParse

repo_file = "/home/nmarques/my_repos.repo"

parser = optparse.OptionParser()
parser.add_option("-e", dest="repository", help="Enable YUM repository")
parser.add_option("-d", dest="repository", help="Disable YUM repository")
parser.add_option("-l", dest="list", help="Display list of repositories", action="store_true")
(options, args) = parser.parse_args()

def list_my_repos()
    # check if repository file exists, read repositories, print and exit
    if os.path.exists(repo_file):
        config = ConfigParser.RawConfigParser()
        config.read(repo_file)
        print "Found the following YUM repositories on " + …
Run Code Online (Sandbox Code Playgroud)

python optparse

6
推荐指数
2
解决办法
893
查看次数

Python - 以奇怪的方式替换字符串;

任何人都可以帮忙吗?想象一下:

def example(s):
    s = s.replace('foo', 'foo bar')
    return s
Run Code Online (Sandbox Code Playgroud)

现在这将用'foo bar'取代'foo'; 但我想做一些不同的事情:*想象一下,我有'foo something'; 我希望最终的结果是:'foo something bar'

做出这种检查的最佳方式是什么(如果有'某事',我想保留它).

有人可以帮忙吗?

NM

python string replace

1
推荐指数
1
解决办法
105
查看次数

标签 统计

python ×2

optparse ×1

replace ×1

string ×1