python系列 文件操作的代码
核心代码 import numpy as np import os,sys #获取当前文件夹,并根据文件名 def path(fileName): p=sys.path[0]+''+fileName return p #读文件 def readFile(fileName): f=open(path(fileName)) str=f.read() f.close() return str #写文件 def writeFile(fileName,str): f=open(path(fileName),'w') f.write(str) f.close() def str1(): str=','.join('我在中国大地上骄傲地生长着!') return str def str2(): return str(np.random.randint(-49,50,[3,3,3])) #实验1 def test_1(): fileName='中国大地.txt' writeFile(fileName,str1()) list=readFile(fileName).split(',') print(list) #实验2 def test_2(): writeFile('str1',str1()) writeFile('str2',str2()) str_1=readFile('str1') str_2=readFile('str2') print(str_1) print(str_2) test_2() 下面是一些 打开和关闭示例: 读取 写入 randint(low[,high,shape]) 根据shape创建随机整数或整数数组,范围是[low,high) numpy.random.randint的详细用法 函数的作用是,返回一个随机整型数,范围从低(包括)到高(不包括),即[low,high)。如果没有写参数high的值,则返回[0,low)的值。 numpy.random.randint(low,high=None,size=None,dtype='l')
输出: 返回一个随机数或随机数数组 例子
好了这篇文章先介绍到这,后续我们小编会为大家分享更多的资料。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |