python 中的re 模块
正则表达式
就个人而言,主要用它来做一些复杂字符串分析,提取想要的信息
学习原则:够用就行,需要的时候在深入
现总结如下:
正则表达式中特殊的符号:
“.” 表任意字符
“^ ” 表string起始
“$” 表string 结束
“*” “+” “?” 跟在字符后面表示,0个——多个, 1个——多个, 0个或者1个
*?, +?, ?? 符合条件的情况下,匹配的尽可能少//限制*,+,?匹配的贪婪性
{m} 匹配此前的字符,重复m次
{m,n} m到n次,m,n可以省略
举个例子 ‘a.*b’ 表示a开始,b结束的任意字符串
a{5} 匹配连续5个a
[] 表一系列字符 [abcd] 表a,b,c,d [^a] 表示非a
| A|B 表示A或者B , AB为任意的正则表达式 另外|是非贪婪的如果A匹配,则不找B
(…) 这个括号的作用要结合实例才能理解, 用于提取信息
\d [0-9]
\D 非 \d
\s 表示空字符
\S 非空字符
\w [a-zA-Z0-9_]
\W 非 \w
一:re的几个函数
1: compile(pattern, [flags])
根据正则表达式字符串 pattern 和可选的flags 生成正则表达式 对象
生成正则表达式 对 ......
真是倒霉,刚买不久的移动硬盘,昨天删除一个分区失败后,几个分区都不见了,拿去修,未果
换了个新的,但其中数据全没了。那是我平时收集的很有用的资料
很多都可以重新下载,但怎能想起硬盘中的所有东西
今天换硬盘回来
就像写一个保存指定路径下所有文件夹和文件名的程序
这样,如果东西丢了,看看那里有些什么,也可聊以告慰
#filesindir.py for files in directory
#result is a txt file with the name of the directory to record
#cmd:filesindir.py "dirname"
# result will be stored in the directory
#or cmd:filesindir.py "dirname" "directory to store the result"
import os
import os.path
import sys
def dirparser(directory,indent,file):
try:
dirlist=os.listdir(directory)
dirli=[]
for i in dirlist:
if(os.path.isdir(directory+'\\'+i)==True):
dirli.append(directory+'\\'+i)
else:
for ind in range(indent):
file.write(' ')
file.write(i+'\n')
for dir in dirl ......
Python的os模块,包含了普遍的操作系统功能,这里主要学习与路径相关的函数:
os.listdir(dirname):列出dirname下的目录和文件
os.getcwd():获得当前工作目录
os.curdir:返回当前目录('.')
os.chdir(dirname):改变工作目录到dirname
os.path.isdir(name):判断name是不是一个目录,name不是目录就返回false
os.path.isfile(name):判断name是不是一个文件,不存在name也返回false
os.path.exists(name):判断是否存在文件或目录name
os.path.getsize(name):获得文件大小,如果name是目录返回0
os.path.abspath(name):获得绝对路径
os.path.normpath(path):规范path字符串形式
os.path.split(name):分割文件名与目录(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)
os.path.splitext():分离文件名与扩展名
os.path.join(path,name):连接目录与文件名或目录
os.path.basename(path):返回文件名
os.path.dirname(path):返回文件路径
1、os.path方法
通过传入需要遍历的目录,列出目录下的所有文件并统计文件数,os提供的path模块能对目录非常灵活的操作 ......
Python代码
import string, os, sys
dir = '/var'
print '----------- no sub dir'
files = os.listdir(dir)
for f in files:
print dir + os.sep + f
print '----------- all dir'
for root, dirs, files in os.walk(dir):
for name in files:
print os.path.join(root, name)
import string, os, sys
dir = '/var'
print '----------- no sub dir'
files = os.listdir(dir)
for f in files:
print dir + os.sep + f
print '----------- all dir'
for root, dirs, files in os.walk(dir):
for name in files:
print os.path.join(root, name)
前面的 os ......
1.可执行程序
os.system('pgrep %s > %s' % (process, output))
pidfile = open("output", 'r')
totalpid = len(pidfile.readlines())
pidfile.close()
if totalpid == 0 :
#没有进程
return False
elif totalpid > 1 :
#多个进程 ......
1.可执行程序
os.system('pgrep %s > %s' % (process, output))
pidfile = open("output", 'r')
totalpid = len(pidfile.readlines())
pidfile.close()
if totalpid == 0 :
#没有进程
return False
elif totalpid > 1 :
#多个进程 ......
#
-*- encoding: gb2312 -*-
import
os, sys, string
import
MySQLdb
#
连接数据库
try
:
conn
=
MySQLdb.connect(host
=
'
localhost
'
,user
=
'
root
'
,passwd
=
'
xxxx
'
,db
=
'
test1
'
)
except
Exception, e:
print
e
sys.exit()
#
获取cursor对象来进行操作
cursor
=
conn.cursor()
#
创建表
sql
=
"
create table if not exists test1(name varchar(128) primary key, age int(4))
"
cursor.execute(sql)
#
插入数据
sql
=
"
insert into test1(name, age) values ('%s', %d)
"
%
(
"
zhaowei
"
,
23
)
try
:
cursor.execute(sql)
except
Exception, e:
print
e
sql
=
"
insert into test1(name, age) values ('%s', %d)
"
%
(
"
张三
"
,
21
)
try
:
cursor.execute(sql)
except
Exception, e:
print
e
# ......
#
-*- encoding: gb2312 -*-
import
os, sys, string
import
MySQLdb
#
连接数据库
try
:
conn
=
MySQLdb.connect(host
=
'
localhost
'
,user
=
'
root
'
,passwd
=
'
xxxx
'
,db
=
'
test1
'
)
except
Exception, e:
print
e
sys.exit()
#
获取cursor对象来进行操作
cursor
=
conn.cursor()
#
创建表
sql
=
"
create table if not exists test1(name varchar(128) primary key, age int(4))
"
cursor.execute(sql)
#
插入数据
sql
=
"
insert into test1(name, age) values ('%s', %d)
"
%
(
"
zhaowei
"
,
23
)
try
:
cursor.execute(sql)
except
Exception, e:
print
e
sql
=
"
insert into test1(name, age) values ('%s', %d)
"
%
(
"
张三
"
,
21
)
try
:
cursor.execute(sql)
except
Exception, e:
print
e
# ......