python系列 文件操作的代碼
核心代碼
import numpy as np
import os,sys
#獲取當(dāng)前文件夾,并根據(jù)文件名
def path(fileName):
p=sys.path[0]+'\\'+fileName
return p
#讀文件
def readFile(fileName):
f=open(path(fileName))
str=f.read()
f.close()
return str
#寫(xiě)文件
def writeFile(fileName,str):
f=open(path(fileName),'w')
f.write(str)
f.close()
def str1():
str=','.join('我在中國(guó)大地上驕傲地生長(zhǎng)著!')
return str
def str2():
return str(np.random.randint(-49,50,[3,3,3]))
#實(shí)驗(yàn)1
def test_1():
fileName='中國(guó)大地.txt'
writeFile(fileName,str1())
list=readFile(fileName).split(',')
print(list)
#實(shí)驗(yàn)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()
下面是一些
打開(kāi)和關(guān)閉示例:

讀取


寫(xiě)入


randint(low[,high,shape]) 根據(jù)shape創(chuàng)建隨機(jī)整數(shù)或整數(shù)數(shù)組,范圍是[low, high)
numpy.random.randint的詳細(xì)用法
函數(shù)的作用是,返回一個(gè)隨機(jī)整型數(shù),范圍從低(包括)到高(不包括),即[low, high)。如果沒(méi)有寫(xiě)參數(shù)high的值,則返回[0,low)的值。
numpy.random.randint(low, high=None, size=None, dtype='l')
參數(shù)如下:
| 參數(shù) | 描述 |
|---|---|
| low: int | 生成的數(shù)值最低要大于等于low。 (hign = None時(shí),生成的數(shù)值要在[0, low)區(qū)間內(nèi)) |
| high: int (可選) | 如果使用這個(gè)值,則生成的數(shù)值在[low, high)區(qū)間。 |
| size: int or tuple of ints(可選) | 輸出隨機(jī)數(shù)的尺寸,比如size=(m * n* k)則輸出同規(guī)模即m * n* k個(gè)隨機(jī)數(shù)。默認(rèn)是None的,僅僅返回滿(mǎn)足要求的單一隨機(jī)數(shù)。 |
| dtype: dtype(可選): | 想要輸出的格式。如int64、int等等 |
輸出:
返回一個(gè)隨機(jī)數(shù)或隨機(jī)數(shù)數(shù)組
例子
>>> 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]])
好了這篇文章先介紹到這,后續(xù)腳本之家小編會(huì)為大家分享更多的資料。
相關(guān)文章
python3發(fā)送request請(qǐng)求及查看返回結(jié)果實(shí)例
這篇文章主要介紹了python3發(fā)送request請(qǐng)求及查看返回結(jié)果實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Python簡(jiǎn)單生成隨機(jī)數(shù)的方法示例
這篇文章主要介紹了Python簡(jiǎn)單生成隨機(jī)數(shù)的方法,結(jié)合實(shí)例形式分析了Python基于random模塊生成隨機(jī)數(shù)的相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
python使用tomorrow實(shí)現(xiàn)多線程的例子
今天小編就為大家分享一篇python使用tomorrow實(shí)現(xiàn)多線程的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
Python定時(shí)發(fā)送天氣預(yù)報(bào)郵件代碼實(shí)例
這篇文章主要介紹了Python定時(shí)發(fā)送天氣預(yù)報(bào)郵件代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
20行Python代碼實(shí)現(xiàn)一款永久免費(fèi)PDF編輯工具的實(shí)現(xiàn)
這篇文章主要介紹了20行Python代碼實(shí)現(xiàn)一款永久免費(fèi)PDF編輯工具的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

