文件读写(python 3.5)

2024-05-09 13:18
文章标签 python 3.5 读写

本文主要是介绍文件读写(python 3.5),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

写文件:

#coding=utf8 #!/usr/bin/env python'makeTextFile.py--create text file'import os
#ls=os.linesep
ls='\n'#get filename
fname='e:/myfile.txt'
while True:if os.path.exists(fname):print("Error:'%s' already exists" % fname)else:break#get file content(text) lines
all=[]
print("\nEnter lines ('.' by itself to quit).\n")#loop until user terminates input
while True:entry=input('> ')if entry=='.':breakelse:all.append(entry)#write lines to file with proper line-ending
fobj=open(fname,'w')
fobj.writelines(['%s%s'%(x,ls) for x in all])
fobj.close()
print('DONE')

读文件:

#!/usr/bin/env python'readTextFile.py--read and display text file'#get filename
fname='e:/1.txt'
fname=input('Enter filename:')
print()#attemp to open file for reading
try:fobj=open(fname,'r')
except IOError as e:print("*** file open error:",e)
else:#display contents to the screenfor eachLine in fobj:print(eachLine,end='')fobj.close() 



这篇关于文件读写(python 3.5)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/973525

相关文章

Android_02_在内部或外部存储中读写文件的操作

前言:  Ram内存:运行内存,相当于电脑的内存  Rom内存:内部存储空间,相当于电脑的硬盘 sd卡:外部存储空间,相当于电脑的移动硬盘 1>在内部存储中读写文件 代码示例如下: package com.itheima.rwinrom;import java.io.BufferedReader;import java.io.File;import java.io.File

STM32 标准库3.5修改默认外部8M晶振为16M晶振

ST官方标准库V3.5默认的外部晶振频率为8M,实际使用中外部晶振需要修改为16M; 经过实验,修改有效,具体的patch如下: 修改 HSE_VALUE 值 diff --git "a/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/stm32f10x.h" "b/Libraries/CMSIS/CM3/DeviceSupport/ST/STM

NOR Flash 读写的高端操作,你看得懂吗?

大家好,我是痞子衡,是正经搞技术的痞子。今天痞子衡给大家介绍的是i.MXRT下改造FlexSPI driver以AHB方式去写入NOR Flash。 痞子衡前段时间写过一篇 《串行NAND Flash的两大特性导致其在i.MXRT FlexSPI下无法XiP》,文章里介绍了 NAND Flash 的 Page Read 等待特性(发完 Read 命令后需要回读 Flash 内部状态寄存器 Bu

Zen of Python -Python之禅

在浏览Python官方文档时无意发现了这个彩蛋,只需在终端中import this The Zen of Python, by Tim PetersBeautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than compli

Python内置函数oct()详解

Python中的oct()函数是一个内置函数,用于将一个整数转换成它的八进制字符串表示。 函数定义 oct()函数的基本语法如下: oct(x) x:一个整数。 函数返回x的八进制表示,以字符串形式。 基本用法 将整数转换为八进制 number = 64print(oct(number)) # 输出: '0o100' 转换负整数 number = -64print(o

Python筑基之旅-溯源及发展

目录 一、Python的起源 二、Python的版本更替及变化 三、Python的优缺点 四、Python的发展方向 五、Python之禅 六、推荐专栏/主页: 1、Python函数之旅:Functions 2、Python算法之旅:Algorithms 3、个人主页:https://myelsa1024.blog.csdn.net/ ​​​​​​​ 一、Python

Python专题:十六、异常处理(2)

异常的预判和防护 import randomnum = random.randint(1, 100) # 获得一个随机数is_done = False # 是否猜中的标记count = 0 # 玩家猜了几次while not is_done:guess = int(input('请输入一个[1, 100]的整数:'))if guess == num:is_done = Trueelif

[Cocos Creator 3.5赛车游戏] 第2节 环境准备

所有教程都避不开环境准备,下面您将要安装好您将会用到的Cocos DashBoard和VS Code,步骤如下: 一.安装Cocos DashBoard: 如果直接百度里输入“Cocos DashBoard 下载”,得到的结果没有一个是有用的,所以还是要自己去官网下载,官网下载地址为:https://www.cocos.com/creator-download,目前界面如下:

理解 Python 中的 `super()` 与 `__init__()` 方法

在 Python 的面向对象编程中,super() 函数和 __init__() 方法是两个非常重要的概念。它们在类的继承和初始化过程中扮演着关键的角色。本文将深入探讨这两个概念的工作原理,并通过示例代码来展示它们的使用。 基本原理 __init__() 方法 __init__() 是一个特殊的方法,也称为类的构造器。当你创建一个类的新实例时,Python 会自动调用这个方法。它通常用于初始

python 合并 pdf

from pypdf import PdfMergerpdfs = ['file1.pdf', 'file2.pdf', 'file3.pdf', 'file4.pdf']merger = PdfMerger()for pdf in pdfs:merger.append(pdf)merger.write("result.pdf")merger.close() 参考 https://stack