import pickle
import numpy as np
x = np.arange(10)
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
f = open("x.pkl","wb")
pickle.dump(x,f)
import os
root = os.getcwd() #获取当前路径
print(root)
# os.walk() 用于在目录树中游走输出目录中的文件名字,向上或下;
def file_name(file_dir):
for root, dirs, files in os.walk(file_dir):
print ("--------------------------------------------------------------")
print (root) #os.walk()所在目录
print (dirs) #os.walk()所在目录的所有目录名
print (files) #os.walk()所在目录的所有非目录文件名
file_name(root) #### 可以看到 x.pkl 已经序列化到硬盘上了
D:\Python_DS_Project
--------------------------------------------------------------
D:\Python_DS_Project
[’.ipynb_checkpoints’]
[‘array的input和output.ipynb’, ‘x.pkl’, ‘数组与矩阵运算.ipynb’, ‘数组创建和访问.ipynb’]
--------------------------------------------------------------
D:\Python_DS_Project.ipynb_checkpoints
[]
[‘array的input和output-checkpoint.ipynb’, ‘数组与矩阵运算-checkpoint.ipynb’, ‘数组创建和访问-checkpoint.ipynb’]
f = open("x.pkl","rb")
pickle.load(f)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.save("one_array",x)
root = os.getcwd() #获取当前路径
# os.walk() 用于在目录树中游走输出目录中的文件名字,向上或下;
def file_name(file_dir):
for root, dirs, files in os.walk(file_dir):
print ("--------------------------------------------------------------")
print (root) #os.walk()所在目录
print (dirs) #os.walk()所在目录的所有目录名
print (files) #os.walk()所在目录的所有非目录文件名
file_name(root) #### 可以看到 one_array.npy 已经序列化到硬盘上了
D:\Python_DS_Project
[’.ipynb_checkpoints’]
[‘array的input和output.ipynb’, ‘one_array.npy’, ‘x.pkl’, ‘数组与矩阵运算.ipynb’, ‘数组创建和访问.ipynb’]
--------------------------------------------------------------
D:\Python_DS_Project.ipynb_checkpoints
[]
[‘array的input和output-checkpoint.ipynb’, ‘数组与矩阵运算-checkpoint.ipynb’, ‘数组创建和访问-checkpoint.ipynb’]
np.load("one_array.npy") #查看文件
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.arange(20)
y
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
np.savez("two_array.npz",a = x, b = y) #序列化多个文件 (通过savez来压缩两个文件)
os.listdir() #访问当前目录下所有文件(不进行迭代)
[’.ipynb_checkpoints’,
‘array的input和output.ipynb’,
‘one_array.npy’,
‘two_array.npz’,
‘x.pkl’,
‘数组与矩阵运算.ipynb’,
‘数组创建和访问.ipynb’]
c = np.load("two_array.npz") #查看文件
c["a"]
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
c["b"]
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])