Python官方文档(中文版)
Numpy
PyTorch中文文档 PyTorch
Pandas
scikit-learn
TensorFlow
Keras
matplotlib
pyecharts
wxpy
获取文件名操作
os.listdir()
语法:os.listdir(path)
函数用途:输出路径下所有文件的文件名,包括文件夹
os.listdir 以列表的形式返回指定文件夹的下所有内容,不管是文件还是文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import os path = "E:/vs_code/qtVideo/lib" datanames = os.listdir(path) for i in datanames: print(i)
输出: A_TEST OpenCVConfig-version.cmake OpenCVConfig.cmake OpenCVModules-debug.cmake OpenCVModules-release.cmake OpenCVModules.cmake opencv_aruco330.lib opencv_aruco330d.lib opencv_bgsegm330.lib opencv_bgsegm330d.lib opencv_bioinspired330.lib opencv_bioinspired330d.lib opencv_calib3d330.lib
|
os.path.splitext()
语法:os.path.splitext(文件名)
函数用途:将文件名和扩展名分开。
就是以文件名中的’.’作为分隔符,分隔文件名称与文件后缀,以元组的形式返回文件名与后缀。
1 2 3 4 5 6 7 8 9 10 11 12 13
| name1 = "opencv_xphoto330d.lib" name2 = "D:\\opencv_xphoto330d.lib" result1 = os.path.splitext(name1) result2 = os.path.splitext(name2) print("result1:",result1) print("result2:",result2) print("type:",type(result1),type(result2))
输出: result1: ('opencv_xphoto330d', '.lib') result2: ('D:\\opencv_xphoto330d', '.lib') type: <class 'tuple'> <class 'tuple'>
|
os.path.split()
语法:os.path.split(path)
函数用途:返回文件的路径和文件名。
就是以路径最后一个’/‘或者’\‘为分隔,以元祖的形式返回路径与文件名,若只有文件名,则输出文件名与空。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| name1 = "opencv_xphoto330d.lib" name2 = "E:\\vs_code\\qtVideo\\opencv_xphoto330d.lib" name3 = "E:\\vs_code\\qtVideo" result1 = os.path.split(name1) result2 = os.path.split(name2) result3 = os.path.split(name3) print("result1:",result1) print("result2:",result2) print("result3:",result3) print("type:",type(result1),type(result2),type(result3))
输出: result1: ('', 'opencv_xphoto330d.lib') result2: ('E:\\vs_code\\qtVideo', 'opencv_xphoto330d.lib') result3: ('E:\\vs_code', 'qtVideo') type: <class 'tuple'> <class 'tuple'>
|
os.path.basename()
1 2 3 4
| import os.path filePath=“K:/Project/FilterDriver/DriverCodes/hello.txt” print(os.path.basename(filePath)) 执行的结果仍然是hello.txt。
|
常用库
Matplotlib
画图
直方图
1 2 3 4 5 6 7 8 9 10 11 12 13
| import matplotlib.mlab as mlab import matplotlib.pyplot as plt
x = [x[0] for x in canzhao] y = [y[1] for y in canzhao]
fig = plt.figure() plt.bar(x,y,0.01,color="green") plt.xlabel("X-w_1") plt.ylabel("Y-rate") plt.title("bar chart")
plt.show()
|
将print输入文件
1 2 3 4
| mylog = open('recode.log', mode = 'a',encoding='utf-8') for i in range(10): print("sdjlahjljag", file=mylog) mylog.close()
|