- cmd模块提供了一个编写交互式shell的功能
方法do_action,action即是一个交互的命令,do_action下面加的一行字符串或者"""多行字符串可以当做action命令的说明,cmdloop方法可以进入交互式shell。
import cmd
class HelloShell(cmd.Cmd):
intro = 'Welcome to the hello shell. Type help or ? to list commands.\n'
prompt = '(hello) '
def do_hello(self, arg):
'hello command will print hello'
print("hello")
def do_exit(self, arg):
'exit the hello shell'
return True
if __name__ == '__main__':
HelloShell().cmdloop()
- python调用系统命令
from subprocess import call
call(["ls", "-l"])
- python中没有switch case,可用如下方式代替
def f(x):
return {
'a': 1,
'b': 2
}.get(x, 9) # 9 is default if x not found
- python中对字符串trim方法
s = " a string example "
s = s.strip()
- python中对字符串list进行join
my_list = ["Hello", "world"]
print "-".join(my_list)
- if name == "main":用法
如果当前文件是命令行中被执行的文件,如python test.py,且test.py中包含上面代码,则test.py中name在解释执行的时候就是main,因此就可以执行if后面的代码,否则name的值是module's name。
https://stackoverflow.com/questions/419163/what-does-if-name-main-do
- 列表append和extend区别
a = [1,2];b = [3,4]
a.append(b)后a变为[1,2,[3,4]]
a.extend(b)后a变为[1,2,3,4]
网友评论