//=================遍历目录
1 # -*- coding: utf-8 -*-
2
3 def cdcGrep(cdcpath,keyword):
4 filelist = os.listdir(cdcpath) # 搜索目录中的文件
5 for cdc in filelist: # 循环文件列表
6 if ".cdc" in cdc: # 过滤可能的其它文件,只关注.cdc
7 cdcfile = open(cdcpath+cdc) # 拼合文件路径,并打开文件
8 for line in cdcfile.readlines(): # 读取文件每一行,并循环CDays“光盘故事” | 29
9 if keyword in line: # 判定是否有关键词在行中
10 print line # 打印输出
1 # -*- coding: utf-8 -*-
2 import os,sys
3 print sys.argv
4 def cdWalker(cdrom,cdcfile):
5 export = ""
6 for root, dirs, files in os.walk(cdrom):
7 export+="\n %s;%s;%s" % (root,dirs,files)
8 open(cdcfile, 'w').write(export)
9 #cdWalker('/media/cdrom0','cd1.cdc')
import cPickle as pickle
path = '/www/album_*'
lt = glob(path)
for z in lt:
gf = open(z,"r")
l = pickle.load(gf)
gf.close()
if type(l) == list :
for i in l:
//===================编码问题
unicode(原始文本, 'utf8' ).encode('utf8')
文本 ==decode()--> [unicode] ==>encode()--> utf-8 文本
^ ^ ^ ^ ^
| | | | +- 最终的渴求
| | | +- 此为编码过程;可以从unicode 输出为任意编码
| | +- Python 内置支持的unicode 格式数据
| +- 此为解码过程,将已知编码的文本编译成宇宙通用的unicode 数据
+- 原始文本信息,是什么编码你得知道!
//================文件模式 操作
r 以读方式打开
Edit By Vheavens
Edit By Vheavens
rU 或 Ua 以读方式打开, 同时提供通用换行符支持 (PEP 278)
w 以写方式打开 (必要时清空)
a 以追加模式打开 (从 EOF 开始, 必要时创建新文件)
r+ 以读写模式打开
w+ 以读写模式打开 (参见 w )
a+ 以读写模式打开 (参见 a )
rb 以二进制读模式打开
wb 以二进制写模式打开 (参见 w )
ab 以二进制追加模式打开 (参见 a )
rb+ 以二进制读写模式打开 (参见 r+ )
wb+ 以二进制读写模式打开 (参见 w+ )
ab+ 以二进制读写模式打开 (参见 a+ )
a. Python 2.3 中新增
name =
'Swaroop'
# This is a string object
if
name.startswith(
'Swa'
)://判断开始字符
print
'Yes, the string starts with "Swa"'
if
'a'
in
name://判断字符是否存在
print
'Yes, it contains the string "a"'
>>> a='123456'
>>> '1' in a
True
>>> a=(1,2,3,4)
>>> 1 in a
True
>>> a=[1,2,3,4]
>>> 1 in a
True
>>> a={1:11,2:22,3:33}
>>> 1 in a
True
if
name.find(
'war'
) !=
-1
://查找字符
print
'Yes, it contains the string "war"'
delimiter =
'_*_'
//链接列表mylist = [
'Brazil'
,
'Russia'
,
'India'
,
'China'
]
print
delimiter.join(mylist)