python学习笔记6-输入输出与文件读取写入
2024-09-30 09:39:04
(1)打印到屏幕:print
(2)读取键盘输入:input/raw_input
#键盘输入
str = raw_input("Please enter:");
print ("你输入的内容是: ", str) str = input("Please enter:");
print ("你输入的内容是: ", str)
(3)打开文件 open 关闭close 读取read
#打开与关闭文件
# 打开一个文件
fo = open("foo.txt", "wb")
print ("文件名: ", fo.name)
print ("是否已关闭 : ", fo.closed)
print ("访问模式 : ", fo.mode)#wb
print ("末尾是否强制加空格 : ", fo.softspace)
# 打开一个文件 不存在就创建
fo = open("foo.txt", "wb")
print ("文件名: ", fo.name)
fo.close() # 打开一个文件
fo = open("foo.txt", "wb")
fo.write( "www.runoob.com!\nVery good site!\n"); # 关闭打开的文件
fo.close() # 打开一个文件
fo = open("foo.txt", "r+")
str = fo.read(10);
print ("读取的字符串是 : ", str)
# 关闭打开的文件
fo.close() # 打开一个文件
fo = open("foo.txt", "r+")
str = fo.read(10);
print ("读取的字符串是 : ", str)
(4)查找位置 定义指针位置
# 查找当前位置
position = fo.tell();
print ("当前文件位置 : ", position) # 把指针再次重新定位到文件开头
position = fo.seek(0, 0);
str = fo.read(10);
print ("重新读取字符串 : ", str)
# 关闭打开的文件
fo.close()
(5)文件重命名和删除
import os # 重命名文件test1.txt到test2.txt。
os.rename( "test1.txt", "test2.txt" ) import os # 删除一个已经存在的文件test2.txt
os.remove("test2.txt")
最新文章
- visual studio 2005 常用按键
- request.querystring和request.form、session的区别
- Android之startActivityForResult的使用
- 十分钟搞懂什么是CGI
- gdb 基本命令
- algorithm@ find the shortest path in a graph using BFS
- JAVA学习:方法
- 利用协议代理实现导航控制器UINavigationController视图之间的正向传值和反向传值
- Animation 的setFillAfter
- ARP欺骗分析
- [java] 在linux+chrome/firefox上使用java applet
- 推箱子 HDU1254 (bfs)
- vue中封装一个全局的弹窗js
- 打通WordPress和微信公众号
- 【LOJ】#2525. 「HAOI2018」字串覆盖
- T-Sql常用语句
- window10下Docker安装
- 使用Kafka、Elasticsearch、Grafana搭建业务监控系统(三)Elasticsearch
- 78W的数据使用forall 进行批量转移;
- mysql 实验
热门文章
- 【spring boot】mybatis启动报错:Consider defining a bean of type 'com.newhope.interview.dao.UserMapper' in your configuration. 【Mapper类不能被找到】@Mapper 和@MapperScan注解的区别
- 大话大前端时代(一) —— Vue 与 iOS 的组件化
- MVC Page分页控件
- FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码(转)
- C++函数模板例子
- 项目问题总结2:GUID区分大写和小写吗?
- 【网站支付PHP篇】thinkPHP集成汇潮支付(ecpss)
- Hadoop如何计算map数和reduce数
- 6.游戏特别离不开脚本(4)-应该避免将集合框架对象传给JS
- DELPHI中的消息处理机制(三种消息处理方法的比较,如何截断消息)