欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python备忘录

程序员文章站 2024-03-23 23:07:34
...

提取某个路径下的所有文件夹的名字

import os
path='./somepath'
l = os.listdir(path)
print l

提取某个路径下所有的文件

from os import walk
f = []
for (dirpath, dirnames, filenames) in walk(mypath):
    f.extend(filenames)
    break

逐行读取文件

 with open(filename) as fp:            
            for line in fp:

复制文件

from shutil import copyfile
copyfile(src, dst)

给字典排序

sorted_cat = sorted(catNinstance.items(), key=operator.itemgetter(1),reverse=True)

matplot cmap

import matplotlib.pyplot as plt

figsim, ax = plt.subplots(figsize=(15,15))
# vmin vmax用于固定颜色范围,确保在不同的figure中,同一个值对应的颜色相同
cax = ax.matshow(sub_prob_matrix, interpolation='nearest',cmap=plt.cm.Blues_r,vmin=0, vmax=1)
ax.grid(True)
#plt.title('Title')
plt.xticks(range(n_w), labels, rotation=90,size=20);
plt.yticks(range(n_w), labels,size =20);
figsim.colorbar(cax, ticks=[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0])
plt.show()
figsim.savefig('name.pdf')
figsim.savefig('name.png')

颜色表选项:
https://matplotlib.org/examples/color/colormaps_reference.html