一. 打印并输出到文件
>>> print("aa","bb"," 11 ","ddddd",sep='-',end='ok/t',file=open("c:/test.txt",'w'))
>>> print("-" *40)#打印40个"-"
>>> print("aa","bb"," 11 ","ddddd",sep='-',end='ok/t',file=open("c:/test.txt",'w'))
>>> print("-" *40)#打印40个"-"
二. 打印字典的值
>>> d = {"a":1,"b":2}
>>> print('%(a)s...%(b)s' %d)
1...2
>>> s = ('%(a)s...%(b)s' %d)
>>> s
'1...2'
>>> s = ['%(a)s...%(b)s' %d]
>>> s
['1...2']
>>> print("%(__builtins__)s...%(d)s" %vars())
<module 'builtins' (built-in)>...{'a': 1, 'b': 2}
>>> vars()
{'__builtins__': <module 'builtins' (built-in)>, '__name__': '__main__', '__doc__': None, 'd': {'a': 1, 'b': 2}, '__package__': None}
>>> locals()
{'__builtins__': <module 'builtins' (built-in)>, '__name__': '__main__', '__doc__': None, 'd': {'a': 1, 'b': 2}, '__package__': None}
>>> vars() is locals() #vars()无参数的情况等同locals()
True
>>> vars() == locals()
True
>>> a = vars()
>>> b = locals()
>>> a is b
True
>>> d = {"a":1,"b":2}
>>> print('%(a)s...%(b)s' %d)
1...2
>>> s = ('%(a)s...%(b)s' %d)
>>> s
'1...2'
>>> s = ['%(a)s...%(b)s' %d]
>>> s
['1...2']
>>> print("%(__builtins__)s...%(d)s" %vars())
<module 'builtins' (built-in)>...{'a': 1, 'b': 2}
>>> vars()
{'__builtins__': <module 'builtins' (built-in)>, '__name__': '__main__', '__doc__': None, 'd': {'a': 1, 'b': 2}, '__package__': None}
>>> locals()
{'__builtins__': <module 'builtins' (built-in)>, '__name__': '__main__', '__doc__': None, 'd': {'a': 1, 'b': 2}, '__package__': None}
>>> vars() is locals() #vars()无参数的情况等同locals()
True
>>> vars() == locals()
True
>>> a = vars()
>>> b = locals()
>>> a is b
True
三. pprint整洁打印
>>> import pprint
>>> pprint.pprint(data)
('this is a string',
[1, 2, 3, 4],
('more tuples', 1.0, 2.3, 4.5),
'this is yet another string')
>>> print(data)
('this is a string', [1, 2, 3, 4], ('more tuples', 1.0, 2.3, 4.5), 'this is yet another string')
>>> pp = pprint.PrettyPrinter(indent=4) #缩进4空格
>>> pp.pprint(data)
( 'this is a string',
[1, 2, 3, 4],
('more tuples', 1.0, 2.3, 4.5),
'this is yet another string')
>>> import pprint
>>> pprint.pprint(data)
('this is a string',
[1, 2, 3, 4],
('more tuples', 1.0, 2.3, 4.5),
'this is yet another string')
>>> print(data)
('this is a string', [1, 2, 3, 4], ('more tuples', 1.0, 2.3, 4.5), 'this is yet another string')
>>> pp = pprint.PrettyPrinter(indent=4) #缩进4空格
>>> pp.pprint(data)
( 'this is a string',
[1, 2, 3, 4],
('more tuples', 1.0, 2.3, 4.5),
'this is yet another string')
四. 打印输出重定向
>>> import sys
>>> sys.stdout = open('c:/log123.txt','w')
>>> print ("aa","bb","1111111112213123")
>>> sys.stdout = sys.__stdout__
>>> import sys
>>> sys.stdout = open('c:/log123.txt','w')
>>> print ("aa","bb","1111111112213123")
>>> sys.stdout = sys.__stdout__
>>> import sys
>>> t = sys.stdout
>>> sys.stdout = open("c:/log123.txt",'w')
>>> print('1234abcd')
>>> sys.stdout = t
>>> print("11")
11
>>>
>>> import sys
>>> t = sys.stdout
>>> sys.stdout = open("c:/log123.txt",'w')
>>> print('1234abcd')
>>> sys.stdout = t
>>> print("11")
11
>>>
五. with方式读取文件直到某行停止
读取文件,直到某一行的文本等于"STOP"停止读取。否则,将一直读取下去。
(注意:停止读取的行文本只能是iter()的第二个参数值,不能包含其他的,空格都不行)
with open("c:/test.txt") as fp:
for line in iter(fp.readline,"STOP"):
print(line)
with open("c:/test.txt") as fp:
for line in iter(fp.readline,"STOP"):
print(line)
六. 搜索目录树中包含有某个字符串的文件名的所有文件列表
>>> import glob
>>> import fileinput
>>> import os
>>> import re
>>> glob.glob("E:/cdr01")
['E:/cdr01']
>>> glob.glob("E:/cdr01/*")#只返回当前cdr01下面的子目录名和文件名(带路径,但不包含目录树中的所有子目录与文件)
['E:/cdr01//20110113', 'E:/cdr01//20110114', 'E:/cdr01//20110115', 'E:/cdr01//20110116', 'E:/cdr01//20110117', 'E:/cdr01//20110118', 'E:/cdr01//20110119', 'E:/cdr01//20110120', 'E:/cdr01//20110121', 'E:/cdr01//20110122', 'E:/cdr01//20110123', 'E:/cdr01//20110124', 'E:/cdr01//cdr01.rar']
>>> [(a,b,c) for a , b , c in os.walk("E:/cdr01")] #os.walk返回目录树中的所有目录与文件,他将每一目录的子目录与文件都按一个元组来返回。a代表当前搜索目录(或者子目录),b代表当前搜索目录下面所包含的子目录列表,c代表当前搜索目录下面的所有文件名列表。(不带路径(但可以通过与元组第一项进行拼接为目录或者文件路径),并且包含目录树中的所有子目录与文件)
[('E:/cdr01', ['20110113', '20110114', '20110115', '20110116', '20110117', '20110118', '20110119', '20110120', '20110121', '20110122', '20110123', '20110124'], ['cdr01.rar']), ('E:/cdr01//20110113', [], ['20110113_6042488744.csv']), ('E:/cdr01//20110114', [], ['20110114_6042488744.csv']), ('E:/cdr01//20110115', [], ['20110115_6042488744.csv']), ('E:/cdr01//20110116', [], ['20110116_6042488744.csv']), ('E:/cdr01//20110117', [], ['20110117_6042488744.csv']), ('E:/cdr01//20110118', [], ['20110118_6042488744.csv']), ('E:/cdr01//20110119', [], ['20110119_6042488744.csv']), ('E:/cdr01//20110120', [], ['20110120_6042488744.csv']), ('E:/cdr01//20110121', [], ['20110121_6042488744.csv']), ('E:/cdr01//20110122', [], ['20110122_6042488744.csv']), ('E:/cdr01//20110123', [], ['20110123_6042488744.csv']), ('E:/cdr01//20110124', [], ['20110124_6042488744.csv'])]
>>> list((os.path.join(a,f)) for a , b , c in os.walk("E:/cdr201101") for f in c if re.search('6042488744',os.path.basename(f))) #E:/cdr201101包含很多CSV文件,此方法将搜索目录树中的文件名包含字符串"6042488744"的文件,并将文件名与路径拼接作为绝对路径返回。
['E:/cdr201101//20110113//20110113_6042488744.csv', 'E:/cdr201101//20110114//20110114_6042488744.csv', 'E:/cdr201101//20110115//20110115_6042488744.csv', 'E:/cdr201101//20110116//20110116_6042488744.csv', 'E:/cdr201101//20110117//20110117_6042488744.csv', 'E:/cdr201101//20110118//20110118_6042488744.csv', 'E:/cdr201101//20110119//20110119_6042488744.csv', 'E:/cdr201101//20110120//20110120_6042488744.csv', 'E:/cdr201101//20110121//20110121_6042488744.csv', 'E:/cdr201101//20110122//20110122_6042488744.csv', 'E:/cdr201101//20110123//20110123_6042488744.csv', 'E:/cdr201101//20110124//20110124_6042488744.csv']
>>> import glob
>>> import fileinput
>>> import os
>>> import re
>>> glob.glob("E:/cdr01")
['E:/cdr01']
>>> glob.glob("E:/cdr01/*")#只返回当前cdr01下面的子目录名和文件名(带路径,但不包含目录树中的所有子目录与文件)
['E:/cdr01//20110113', 'E:/cdr01//20110114', 'E:/cdr01//20110115', 'E:/cdr01//20110116', 'E:/cdr01//20110117', 'E:/cdr01//20110118', 'E:/cdr01//20110119', 'E:/cdr01//20110120', 'E:/cdr01//20110121', 'E:/cdr01//20110122', 'E:/cdr01//20110123', 'E:/cdr01//20110124', 'E:/cdr01//cdr01.rar']
>>> [(a,b,c) for a , b , c in os.walk("E:/cdr01")] #os.walk返回目录树中的所有目录与文件,他将每一目录的子目录与文件都按一个元组来返回。a代表当前搜索目录(或者子目录),b代表当前搜索目录下面所包含的子目录列表,c代表当前搜索目录下面的所有文件名列表。(不带路径(但可以通过与元组第一项进行拼接为目录或者文件路径),并且包含目录树中的所有子目录与文件)
[('E:/cdr01', ['20110113', '20110114', '20110115', '20110116', '20110117', '20110118', '20110119', '20110120', '20110121', '20110122', '20110123', '20110124'], ['cdr01.rar']), ('E:/cdr01//20110113', [], ['20110113_6042488744.csv']), ('E:/cdr01//20110114', [], ['20110114_6042488744.csv']), ('E:/cdr01//20110115', [], ['20110115_6042488744.csv']), ('E:/cdr01//20110116', [], ['20110116_6042488744.csv']), ('E:/cdr01//20110117', [], ['20110117_6042488744.csv']), ('E:/cdr01//20110118', [], ['20110118_6042488744.csv']), ('E:/cdr01//20110119', [], ['20110119_6042488744.csv']), ('E:/cdr01//20110120', [], ['20110120_6042488744.csv']), ('E:/cdr01//20110121', [], ['20110121_6042488744.csv']), ('E:/cdr01//20110122', [], ['20110122_6042488744.csv']), ('E:/cdr01//20110123', [], ['20110123_6042488744.csv']), ('E:/cdr01//20110124', [], ['20110124_6042488744.csv'])]
>>> list((os.path.join(a,f)) for a , b , c in os.walk("E:/cdr201101") for f in c if re.search('6042488744',os.path.basename(f))) #E:/cdr201101包含很多CSV文件,此方法将搜索目录树中的文件名包含字符串"6042488744"的文件,并将文件名与路径拼接作为绝对路径返回。
['E:/cdr201101//20110113//20110113_6042488744.csv', 'E:/cdr201101//20110114//20110114_6042488744.csv', 'E:/cdr201101//20110115//20110115_6042488744.csv', 'E:/cdr201101//20110116//20110116_6042488744.csv', 'E:/cdr201101//20110117//20110117_6042488744.csv', 'E:/cdr201101//20110118//20110118_6042488744.csv', 'E:/cdr201101//20110119//20110119_6042488744.csv', 'E:/cdr201101//20110120//20110120_6042488744.csv', 'E:/cdr201101//20110121//20110121_6042488744.csv', 'E:/cdr201101//20110122//20110122_6042488744.csv', 'E:/cdr201101//20110123//20110123_6042488744.csv', 'E:/cdr201101//20110124//20110124_6042488744.csv']
七. 字符串转成字节数组,字节数组转成字节串
>>> s = 'abcd123'
>>> b = bytearray(s,sys.stdin.encoding) #sys.stdin.encoding相当于locale.getdefaultlocale()[1]获得当前操作系统的编码方式
>>> b
bytearray(b'abcd123')
>>> b = bytes(s,sys.stdin.encoding)
>>> b
b'abcd123'
>>> s = 'abcd123'
>>> b = bytearray(s,sys.stdin.encoding) #sys.stdin.encoding相当于locale.getdefaultlocale()[1]获得当前操作系统的编码方式
>>> b
bytearray(b'abcd123')
>>> b = bytes(s,sys.stdin.encoding)
>>> b
b'abcd123'
八. 字符串与字节的编码与解码
>>> a = b'abc'
>>> a.decode() #字节解码为字符串
'abc'
>>> b = aa.encode() #字符串编码为字节
>>> b
b'abc'
>>>
>>> a = b'abc'
>>> a.decode() #字节解码为字符串
'abc'
>>> b = aa.encode() #字符串编码为字节
>>> b
b'abc'
>>>
九. 处理zip压缩文件
>>> import zipfile
>>> f = zipfile.ZipFile("E:/CodeBuilder.zip","r")
>>> for name in f.namelist():
data = f.read(name)
print(name,len(data),repr(data[:10]))
>>> f.close()
>>> import zipfile
>>> f = zipfile.ZipFile("E:/CodeBuilder.zip","r")
>>> for name in f.namelist():
data = f.read(name)
print(name,len(data),repr(data[:10]))
>>> f.close()
>>> import zipfile
>>> z = zipfile.ZipFile("E:/test1.zip","w") #创建zip压缩文件包
>>> z.write("E:/testSqliteDB.db.txt") #压缩一个txt文件
>>> z.close()
>>> z = zipfile.ZipFile("E:/test1.zip","r") #读取zip压缩文件包
>>> z.extractall()#解压到当前目录,extract可以具体指定解压哪个文件到哪个目录
>>> z.extractall("E:/123/")#解压到指定目录
>>> z.close()
>>> import os
>>> os.getcwd() #getcwd()可以查看解压的当前目录位置
'C://Python31'
>>> import zipfile
>>> z = zipfile.ZipFile("E:/test1.zip","w") #创建zip压缩文件包
>>> z.write("E:/testSqliteDB.db.txt") #压缩一个txt文件
>>> z.close()
>>> z = zipfile.ZipFile("E:/test1.zip","r") #读取zip压缩文件包
>>> z.extractall()#解压到当前目录,extract可以具体指定解压哪个文件到哪个目录
>>> z.extractall("E:/123/")#解压到指定目录
>>> z.close()
>>> import os
>>> os.getcwd() #getcwd()可以查看解压的当前目录位置
'C://Python31'
十. glob遍历文件夹下面的所有文件并读取内容
>>> import fileinput
>>> import glob
>>> for line in fileinput.input(glob.glob("E:/TestFolder/*.txt")):
print("File name:" + fileinput.filename() + " line length:" + str(len(line)))
>>> fileinput.close()
>>> import fileinput
>>> import glob
>>> for line in fileinput.input(glob.glob("E:/TestFolder/*.txt")):
print("File name:" + fileinput.filename() + " line length:" + str(len(line)))
>>> fileinput.close()
十一. shutil拷贝目录树
>>> import shutil
>>> shutil.copytree("E:/TestFolder","F:/TestFolder-bak")
>>> shutil.copytree("E:/TestFolder","xx") #在TestFolder文件夹里面创建XX目录,并拷贝TestFolder目录到XX目录
>>> import shutil
>>> shutil.copytree("E:/TestFolder","F:/TestFolder-bak")
>>> shutil.copytree("E:/TestFolder","xx") #在TestFolder文件夹里面创建XX目录,并拷贝TestFolder目录到XX目录
十二. 获得文件大小的两种方式(二进制读取)
>>> import os
>>> os.stat(f).st_size
4753
>>> os.path.getsize(f)
4753
>>> import os
>>> os.stat(f).st_size
4753
>>> os.path.getsize(f)
4753
十三. mmap 模块提供了操作系统内存映射函数的接口,可以映射字符串与文件内容
可以执行譬如查找、统计、写入等一些操作,但就是基于字节的。
>>> map = mmap.mmap(-1,13)
>>> map.write(b"Hello world!")
>>> map.readline()
b'/x00'
>>> map.seek(0)
>>> map.readline()
b'Hello world!/x00'
>>> map.seek(0)
>>> map.find(b'w')
6
>>> map.close()
>>> map = mmap.mmap(-1,13)
>>> map.write(b"Hello world!")
>>> map.readline()
b'/x00'
>>> map.seek(0)
>>> map.readline()
b'Hello world!/x00'
>>> map.seek(0)
>>> map.find(b'w')
6
>>> map.close()
十四. dis反编译对象为字节码
>>> import dis
>>> def showMsg(xx):
print("abc")
>>> dis.dis(showMsg)
2 0 LOAD_GLOBAL 0 (print)
3 LOAD_CONST 1 ('abc')
6 CALL_FUNCTION 1
9 POP_TOP
10 LOAD_CONST 0 (None)
13 RETURN_VALUE
>>> import dis
>>> def showMsg(xx):
print("abc")
>>> dis.dis(showMsg)
2 0 LOAD_GLOBAL 0 (print)
3 LOAD_CONST 1 ('abc')
6 CALL_FUNCTION 1
9 POP_TOP
10 LOAD_CONST 0 (None)
13 RETURN_VALUE
十五。profile 与 cProfile分析程序执行性能
profile 模块是标准 Python 分析器,用Python写的。
而cProfile是用C写的,运行起来比profile快,官方推荐用cProfile.
pstats可以处理分析报告。
>>> import profile
>>> def testss(n):
i = 0
for i in range(n):
i = i + 1
return i
>>> profile.run("testss(1000000)")
5 function calls in 0.269 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.266 0.266 :0(exec)
1 0.002 0.002 0.002 0.002 :0(setprofile)
1 0.266 0.266 0.266 0.266 <pyshell#3>:1(testss)
1 0.000 0.000 0.266 0.266 <string>:1(<module>)
0 0.000 0.000 profile:0(profiler)
1 0.000 0.000 0.269 0.269 profile:0(testss(1000000))
>>> import cProfile
>>> cProfile.run("testss(1000000)") #分析速度快些
4 function calls in 0.212 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.212 0.212 0.212 0.212 <pyshell#3>:1(testss)
1 0.000 0.000 0.212 0.212 <string>:1(<module>)
1 0.000 0.000 0.212 0.212 {built-in method exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
>>> import profile
>>> def testss(n):
i = 0
for i in range(n):
i = i + 1
return i
>>> profile.run("testss(1000000)")
5 function calls in 0.269 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.266 0.266 :0(exec)
1 0.002 0.002 0.002 0.002 :0(setprofile)
1 0.266 0.266 0.266 0.266 <pyshell#3>:1(testss)
1 0.000 0.000 0.266 0.266 <string>:1(<module>)
0 0.000 0.000 profile:0(profiler)
1 0.000 0.000 0.269 0.269 profile:0(testss(1000000))
>>> import cProfile
>>> cProfile.run("testss(1000000)") #分析速度快些
4 function calls in 0.212 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.212 0.212 0.212 0.212 <pyshell#3>:1(testss)
1 0.000 0.000 0.212 0.212 <string>:1(<module>)
1 0.000 0.000 0.212 0.212 {built-in method exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
pstats处理分析报告:
>>> import pstats
>>> pr = profile.Profile()
>>> pr.run("testss(1000000)")
<profile.Profile object at 0x0143C8D0>
>>> p = pstats.Stats(pr)
>>> pstats = p.sort_stats("time") #按时间排序
>>> pstats.print_stats() #打印在profile.run()中形成的分析报告
5 function calls in 38.856 CPU seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 25.843 25.843 25.843 25.843 :0(setprofile)
1 12.760 12.760 38.856 38.856 profile:0(testss(1000000))
1 0.253 0.253 0.253 0.253 <pyshell#3>:1(testss)
1 0.000 0.000 0.253 0.253 :0(exec)
1 0.000 0.000 0.253 0.253 <string>:1(<module>)
0 0.000 0.000 profile:0(profiler)
<pstats.Stats object at 0x0143C330>
>>> import pstats
>>> pr = profile.Profile()
>>> pr.run("testss(1000000)")
<profile.Profile object at 0x0143C8D0>
>>> p = pstats.Stats(pr)
>>> pstats = p.sort_stats("time") #按时间排序
>>> pstats.print_stats() #打印在profile.run()中形成的分析报告
5 function calls in 38.856 CPU seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 25.843 25.843 25.843 25.843 :0(setprofile)
1 12.760 12.760 38.856 38.856 profile:0(testss(1000000))
1 0.253 0.253 0.253 0.253 <pyshell#3>:1(testss)
1 0.000 0.000 0.253 0.253 :0(exec)
1 0.000 0.000 0.253 0.253 <string>:1(<module>)
0 0.000 0.000 profile:0(profiler)
<pstats.Stats object at 0x0143C330>