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()
知识点讲解:python系列——文件操作
python系列——文件操作的代码的更多相关文章
- python系列——文件操作
打开和关闭 示例:python系列——文件操作的代码 打开模式 读取 写入
- [转]python file文件操作--内置对象open
python file文件操作--内置对象open 说明: 1. 函数功能打开一个文件,返回一个文件读写对象,然后可以对文件进行相应读写操作. 2. file参数表示的需要打开文件的相对路径(当前 ...
- Python :open文件操作,配合read()使用!
python:open/文件操作 open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) 如何打开文件 handle=open(file_name,a ...
- C#各种文件操作的代码与注释
C#各种文件操作的代码与注释,具体看下面代码: using System; using System.Collections.Generic; using System.Linq; using Sys ...
- Python 常见文件操作的函数示例(转)
转自:http://www.cnblogs.com/txw1958/archive/2012/03/08/2385540.html # -*-coding:utf8 -*- ''''' Python常 ...
- 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容
孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...
- 孤荷凌寒自学python第三十三天python的文件操作初识
孤荷凌寒自学python第三十三天python的文件操作初识 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天开始自学python的普通 文件操作部分的内容. 一.python的文件打开 ...
- python中文件操作的六种模式及对文件某一行进行修改的方法
一.python中文件操作的六种模式分为:r,w,a,r+,w+,a+ r叫做只读模式,只可以读取,不可以写入 w叫做写入模式,只可以写入,不可以读取 a叫做追加写入模式,只可以在末尾追加内容,不可以 ...
- python中文件操作的其他方法
前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r', ...
随机推荐
- 攻防世界--crackme
测试文件:https://adworld.xctf.org.cn/media/task/attachments/088c3bd10de44fa988a3601dc5585da8.exe 1.准备 获取 ...
- Intellij 选择profile
注意有3个地方需要改
- day04 列表增删改查、元祖以及range
01 课前小甜点 千万不要随意做决定 只要你做了决定,你要坚持下去. 02 昨日内容回顾 int <---> bool : 非0 True 0 False True 1 False 0 i ...
- springboot创建一个服务,向eureka中注册,使用swagger2进行服务管理
首先pom.xml文件,spring boot.springcloud版本很麻烦,容易出问题 <?xml version="1.0" encoding="UTF-8 ...
- Django--Auth模块使用
1.Auth模块介绍 1.1 Auth模块是Django自带的用户认证模块,用于处理用户账户.群组.许可和基于cookie的用户回话 Django的认证系统主要包括下面几个部分 1.用户 2.许可 3 ...
- git提交代码冲突解决
1.git stash save "说明信息" 2.git stash lish 3.git pull 4.git stash pop stash@{编号}
- CF 49E Common ancestor
传送门 模拟赛T1就自闭了( 才不会说我是去刚T2了来着 感觉非常暴力的一个题? 出题人良心开大数据范围 n=100 还是原来的n^4*26算法我也是自闭了 不过貌似跑不到n^4? 真·大力出奇迹 一 ...
- vs2005下opengl(glut)的配置记录
摘自:http://blog.csdn.net/joeblackzqq/article/details/6956959 首先参考了网上的安装配置环境部分:http://blog.csdn.net/Id ...
- 【leetcode】623. Add One Row to Tree
题目如下: Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with ...
- js append()和appendChild()和insertBefore()的区别
<body> <input type="button" value="删除" id="btn"> <scrip ...