溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python中文件操作有哪些

發(fā)布時(shí)間:2021-08-25 11:10:06 來源:億速云 閱讀:141 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)python中文件操作有哪些的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

核心代碼

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
 
#寫文件 
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]))

#實(shí)驗(yàn)1 
def test_1():
 fileName='中國大地.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()

下面是一些

打開和關(guān)閉示例:

python中文件操作有哪些

讀取

python中文件操作有哪些

python中文件操作有哪些

寫入

python中文件操作有哪些

python中文件操作有哪些

randint(low[,high,shape]) 根據(jù)shape創(chuàng)建隨機(jī)整數(shù)或整數(shù)數(shù)組,范圍是[low, high)

numpy.random.randint的詳細(xì)用法

函數(shù)的作用是,返回一個隨機(jī)整型數(shù),范圍從低(包括)到高(不包括),即[low, high)。如果沒有寫參數(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 (可選)如果使用這個值,則生成的數(shù)值在[low, high)區(qū)間。
size: int or tuple of ints(可選)輸出隨機(jī)數(shù)的尺寸,比如size=(m * n* k)則輸出同規(guī)模即m * n* k個隨機(jī)數(shù)。默認(rèn)是None的,僅僅返回滿足要求的單一隨機(jī)數(shù)。
dtype: dtype(可選):想要輸出的格式。如int64、int等等

輸出:

返回一個隨機(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]]) 

感謝各位的閱讀!關(guān)于“python中文件操作有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI