常用的Python命令
来源:互联网 发布:2016年上证指数数据 编辑:程序博客网 时间:2024/04/26 08:01
1)调用系统命令
调用SHELL命令nslookup,将执行的结果保存到变量result_nslook中
import oscmd='nslookup %s' % hostnamehandle=os.popen(cmd , 'r')result_nslook=handle.read()调用shell命令但是不需要获取返回结果
import oscmd='ls'os.system(cmd)
2)Python 的字符串处理
去掉前后空格
input = open('hostlist_ip' , 'r')for hostname in input.xreadlines():#按行读文件到hostname中 hostname=hostname.strip() hostlist.append(hostname)input.close()将读入的一个字符串数组按照空格划分为list
temp=display.split()for seg in temp: if seg[0:5]=='time=': output.write(seg.lstrip('time=').strip()+'\t') delayRec.append(seg.lstrip('time='))3)文件操作
打开文件进行读写
import osinput=open(filename ,'r') #读文件output=open(filename ,'w') #写文件output=open(filename,'a') #追加写文件遍历文件夹操作
import osfor root, dirs, files in os.walk(path, topdown=False):#hanlde filefor name in files:if name[:-3] = 'exe':print name#删除文件top='mydata/'for root,dir,files in os.walk(top,topdown=False): for name in files: os.remove(os.path.join(root,name))os.rmdir('mydata')os.mkdir('mydata')列出文件
import osos.listdir("c:\\music\\_singles\\") ['a_time_long_forgotten_con.mp3', 'hellraiser.mp3', 'kairo.mp3','long_way_home1.mp3', 'sidewinder.mp3', 'spinning.mp3']4)如何将Blob存储到SQLite中
有时候我们需要将Blob或者二进制文件对象存储到SQLite数据库中,下面这个例子演示了,Python中是如何实现的:
import osimport sqliteclass Blob: """Automatically encode a binary string.""" def __init__(self, s): self.s = s def _quote(self): return "'%s'" % sqlite.encode(self.s)db = sqlite.connect("test.db")cursor = db.cursor()cursor.execute("CREATE TABLE t (b BLOB);")s = "\0"*50 + "'"*50cursor.execute("INSERT INTO t VALUES(%s);", Blob(s))cursor.execute("SELECT b FROM t;")b = cursor.fetchone()[0]assert b == s # b is automatically decodeddb.close()os.remove("test.db")
- 常用的Python命令
- python的pip命令常用操作
- python中一些常用的函数命令
- python中常用的MySQL操作以及命令
- python中字典的常用操作命令及注意事项
- Linux服务器上Python常用包的安装命令
- Python 常用代码及命令
- python常用文件处理命令
- Linux 我常用的命令 以及如何用python实现这些命令
- 常用的数据库命令
- 常用的网络命令
- 常用的网络命令
- 網絡檢查常用的命令
- 常用的vi命令
- 常用的Linux命令
- debug常用的命令
- 常用的DOS命令
- 常用的网络命令
- 进程和线程的区别
- 体内湿气不排出,永远睡不饱
- 从linux内核启动说起(二)
- 红黑树插入的栈实现
- A+B Format (20)
- 常用的Python命令
- 获取android源码时repo的错误
- 方法签名
- VS2010 Qt配置
- Svnsync如何实现同步,以及出现的各种问题?
- C#中多线程的参数传递
- 用c写的一个栈的链表实现
- hibernate主键生成策略generator
- 给对话框添加菜单 工具栏 状态栏简易方法