python代码文件,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()

下面是一些

打开和关闭示例:

ba5f1554a916c1fe830f9aca749cafe6.png

读取

0ee27e535ad891b54f8a417bd33b5ce1.png

bb8deab0e2eb3222ee3b71409bbf7f56.png

写入

e6d35d10204d3cc8b99e9d7ebefac765.png

a0635c870bcc707f8ff3e2ca37b27655.png

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')

参数如下:

参数

描述

low: int

生成的数值最低要大于等于low。

(hign = None时,生成的数值要在[0, low)区间内)

high: int (可选)

如果使用这个值,则生成的数值在[low, high)区间。

size: int or tuple of ints(可选)

输出随机数的尺寸,比如size=(m * n* k)则输出同规模即m * n* k个随机数。默认是None的,仅仅返回满足要求的单一随机数。

dtype: dtype(可选):

想要输出的格式。如int64、int等等

输出:

返回一个随机数或随机数数组

例子

>>> np.random.randint(2, size=10)

array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])

>>> np.random.randint(1, size=10)

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

>>> np.random.randint(5, size=(2, 4))

array([[4, 0, 2, 1],

[3, 2, 2, 0]])

>>>np.random.randint(2, high=10, size=(2,3))

array([[6, 8, 7],

[2, 5, 2]])

好了这篇文章先介绍到这,后续脚本之家小编会为大家分享更多的资料。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值