小白学Python(二) 基本文件操作
来源:互联网 发布:ansys15.0软件 编辑:程序博客网 时间:2024/05/22 06:57
这是基本的文件操作示例,是《Python核心编程》上的样例程序对Python3.4的适配版,如有不足请诸君斧正
一、编写程序创建新文件并向文件内写入内容
"""makeTextFile.py use this to create a new file""""""You need to open the file with notepad to check the line break"""import osls = os.linesep#get file namewhile True:fname = input("\nPlease input the file name:\n>")if os.path.exists(fname):print("ERROR: " + '%s' %fname + " already existes")else:break#get file content linesall = []print("\nEnter lines ('.' by itself to quit).\n")while True:line = input('>')if line == '.':breakelse:all.append(line)#write to filewfile = open(fname, 'w')for each in all:wfile.write(each + ls)wfile.close()print("DONE!")
二、编写程序打开文件并将内容打印至屏幕
"""Display text file"""import osfname = input("Please input the file name\n>")with open(fname, 'r') as rfile:for each_line in rfile:print(each_line)rfile.close()
0 0
- 小白学Python(二) 基本文件操作
- Python基础--基本文件操作二
- 二、python基本操作
- python 文件操作(二)
- Python 文件操作(二)
- Python文件基本操作
- python基本文件操作
- Python 文件基本操作
- Python 文件基本操作
- Python文件基本操作
- Python基本文件操作
- python基本操作总结(二)
- 文件IO的基本操作(二)
- Python文件操作二
- 文件的基本操作二
- python读写文件基本操作
- Python基础--基本文件操作
- Python 中的基本文件操作
- 【模板整合】SAM后缀自动机的构建
- Android Exception : JAVA_HOME is set to an invalid directory
- 使用fiddler的断点调试抓包修改请求和响应
- 期望和方差
- [LeetCode] Product of Array Except Self
- 小白学Python(二) 基本文件操作
- Android开发学习笔记之Activity属性设置
- 用VS 2015写C++代码:体验一下变参模板、完美转发和lambda
- hdu 5289
- Controller激活系统详解 — IoC的应用
- 如何实现人生的三大自由
- Android项目文件结构
- PHPMEMCACHE安装
- Redis 安装与简单示例 <第一篇>