使用子进程运行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".
我有一个字符串,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)
有一个更好的方法吗?谢谢!
我从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) 我的 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 类(因为它们未在我的项目中使用)?
有某种包装方法可以推荐吗?