小编Luk*_*uke的帖子

保存子进程命令的错误消息

使用子进程运行bash命令时,我可能会遇到命令无效的情况.在这种情况下,bash将返回错误消息.我们怎样才能抓住这条消息?我想将此消息保存到日志文件中.以下是一个示例,我尝试在不存在的目录中列出文件.

try:
    subprocess.check_call(["ls", "/home/non"]) 
    df = subprocess.Popen(["ls", "/home/non"], stdout=subprocess.PIPE)        
    output, err = df.communicate()
    # process outputs
except Exception as error:        
    print error
    sys.exit(1)
Run Code Online (Sandbox Code Playgroud)

Bash会打印"ls:无法访问/ home/non:没有这样的文件或目录".我怎样才能收到此错误消息?except行捕获的错误明显不同,它说"Command'['ls','/ home/non']'返回非零退出状态2".

python error-handling bash subprocess

11
推荐指数
2
解决办法
1万
查看次数

检查字符串A中是否没有多个字符?

我有一个字符串,A = "abcdef"和几个字符"a","f""m".我想要一个条件,以确保没有出现的字符A,即

if a not in A and f not in A and m not in A:
    # do something
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?谢谢!

python

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

将字符串"> 0"转换为python> 0

我从xml文件中读取条件"> 0","<60"等.将它们转换为python语言进行比较的最佳方法是什么?示例代码是我想要做的:

    if str == ">0":
        if x > 0:
            print "yes"
        else:
            print "no"
    elif str == "<60":
        if x < 60:
            print "yes"
    ...
Run Code Online (Sandbox Code Playgroud)

python

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

C++ 使头文件的一部分对客户可见

我的 C++ 软件使用另一个收集常见类/函数的存储库。很多时候,我的软件只使用一个头文件中提供的几个相关类之一。假设外部存储库有一个文件“data_types.h”,包含三个类。

class X { // definition }

class Y { // definition }

class Z { // definition }
Run Code Online (Sandbox Code Playgroud)

我的项目使用class Y来自标题。当发布给客户时,我需要包含我的 lib 文件以及 this 的定义class Y。如何向客户隐藏 X 类和 Z 类(因为它们未在我的项目中使用)?

有某种包装方法可以推荐吗?

c++

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

标签 统计

python ×3

bash ×1

c++ ×1

error-handling ×1

subprocess ×1