python系列 文件操作的代码


Posted in Python onOctober 06, 2019

核心代码

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

下面是一些

打开和关闭示例:

python系列 文件操作的代码

读取

python系列 文件操作的代码

python系列 文件操作的代码

写入

python系列 文件操作的代码

python系列 文件操作的代码

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

好了这篇文章先介绍到这,后续三水点靠木小编会为大家分享更多的资料。

Python 相关文章推荐
python difflib模块示例讲解
Sep 13 Python
Python基于whois模块简单识别网站域名及所有者的方法
Apr 23 Python
python腾讯语音合成实现过程解析
Aug 01 Python
对python中的os.getpid()和os.fork()函数详解
Aug 08 Python
django项目中使用手机号登录的实例代码
Aug 15 Python
python画蝴蝶曲线图的实例
Nov 21 Python
python lambda函数及三个常用的高阶函数
Feb 05 Python
python——全排列数的生成方式
Feb 26 Python
解决pycharm导入本地py文件时,模块下方出现红色波浪线的问题
Jun 01 Python
Python引入多个模块及包的概念过程解析
Sep 21 Python
将不规则的Python多维数组拉平到一维的方法实现
Jan 11 Python
pandas针对excel处理的实现
Jan 15 Python
pip 安装库比较慢的解决方法(国内镜像)
Oct 06 #Python
Anaconda之conda常用命令介绍(安装、更新、删除)
Oct 06 #Python
Python pip 安装与使用(安装、更新、删除)
Oct 06 #Python
python 3.74 运行import numpy as np 报错lib\site-packages\numpy\__init__.py
Oct 06 #Python
windows下Python安装、使用教程和Notepad++的使用教程
Oct 06 #Python
Django学习之文件上传与下载
Oct 06 #Python
Django实现文件上传和下载功能
Oct 06 #Python
You might like
CPU步进是什么意思?i3-9100F B0步进和U0步进区别知识科普
2020/03/17 数码科技
调试PHP程序的多种方法介绍
2014/11/06 PHP
表单提交验证类
2006/07/14 Javascript
js DOM 元素ID就是全局变量
2012/09/20 Javascript
JS.getTextContent(element,preformatted)使用介绍
2013/09/21 Javascript
纯js和css实现渐变色包括静态渐变和动态渐变
2014/05/29 Javascript
js阻止事件追加的具体实现
2014/10/15 Javascript
jQuery插件Tmpl的简单使用方法
2015/04/27 Javascript
jqGrid 学习笔记整理——进阶篇(一 )
2016/04/17 Javascript
window.onload绑定多个事件的两种解决方案
2016/05/15 Javascript
JavaScript中setter和getter方法介绍
2016/07/11 Javascript
分享jQuery封装好的一些常用操作
2016/07/28 Javascript
JS简单实现点击复制链接的方法
2016/08/03 Javascript
js仿小米官网图片轮播特效
2016/09/29 Javascript
jquery使用iscorll实现上拉、下拉加载刷新
2017/10/26 jQuery
Vue 获取数组键名的方法
2018/06/21 Javascript
微信小程序自定义轮播图
2018/11/04 Javascript
js+html+css实现手动轮播和自动轮播
2020/12/30 Javascript
flask入门之文件上传与邮件发送示例
2018/07/18 Python
python实现微信定时每天和女友发送消息
2019/04/29 Python
pandas 数据索引与选取的实现方法
2019/06/21 Python
Python+Tensorflow+CNN实现车牌识别的示例代码
2019/10/11 Python
python实现音乐播放和下载小程序功能
2020/04/26 Python
Python3爬虫里关于识别微博宫格验证码的知识点详解
2020/07/30 Python
Python将字典转换为XML的方法
2020/08/01 Python
html5中去掉input type date默认样式的方法
2018/09/06 HTML / CSS
中国高端家电购物商城:顺电
2018/03/04 全球购物
英国电视和家用电器购物网站:rlrdistribution.co.uk
2018/11/20 全球购物
美国优质马术服装购买网站:Breeches.com
2019/12/16 全球购物
Whistles官网:英国女装品牌
2020/08/14 全球购物
一套Java笔试题
2016/08/20 面试题
洗发水广告词
2014/03/13 职场文书
2014年重阳节活动策划方案书
2014/09/16 职场文书
2015年师德表现自我评价
2015/03/05 职场文书
小学生优秀作文范文(六篇)
2019/07/10 职场文书
企业内部管理控制:银行存款控制制度范本
2020/01/10 职场文书