python 数据类型:文件
来源:互联网 发布:iptv网络电视apk下载 编辑:程序博客网 时间:2024/05/29 12:13
open(filename,mode,bufsize)
其参数的意义:
filename:要打开的文件;
mode:可选参数,文件打开的模式。'r' 只读方式打开,'w'写方式打开,'b'表示以二进制方式打开
bufsize: 可选参数,缓冲区大小。
常用的文件操作函数如下:
file.read() 将整个文件读入字符串中
file.readline() 读入文件中的一行字符串
file.readlines() 将整个文件按行读入列表中。
file.write() 向文件中写入字符串
file.writelines() 向文件中写入一个列表
file.close() 关闭打开的文件
- >>> str
- 'abcdefg'
- >>> file = open('c:/python.txt','w')
- >>> file.write('python') #向文件中写入字符串
- >>> a = [] #定义一个空的列表
- >>> for i in range(7):
- ... s = str[i] + '/n
- File "<stdin>", line 2
- s = str[i] + '/n
- ^
- SyntaxError: EOL while scanningstring literal
- >>> for i in range(7):
- ... s = str[i] + '/n'
- ... a.append(s)
- ...
- >>> file.writelines(a) #将列表写入文件
- >>> file.close()
- >>> file = open('c:/python.txt','r')
- >>> s = file.read() #将整个文件读入字符串中
- >>> print s
- pythona
- b
- c
- d
- e
- f
- g
- >>> file.close()
- >>> file = open('c:/python.txt','r')
- >>> l = file.readlines() #将文件读取到列表中
- >>> print l
- ['pythona/n', 'b/n','c/n', 'd/n','e/n', 'f/n','g/n']
- python 数据类型:文件
- python 数据类型:文件
- python3之python的核心数据类型(文件)
- python --- 字符串、数据类型、格式化、文件打开
- Python 数据类型
- python--数据类型
- python 数据类型
- python数据类型
- Python---数据类型
- python数据类型
- python数据类型
- [python] 数据类型
- python数据类型
- python 数据类型
- Python数据类型
- Python数据类型
- Python-数据类型
- Python数据类型
- queue_delayed_work和queue_work区别
- 写正确函数需要注意的地方:链表的合并排序
- Java_乔晓松_Java网络编程UDP实现黑窗口聊天
- Automake使用第三方库的方法
- sql左连接时发生字符集错误Cannot resolve the collation conflict between in the equal to operation.
- python 数据类型:文件
- [C++]使用TinyXml讀寫Xml
- DbgPrint打印字符串
- 哪些年遇到过的Andriod问题(11)如何下载文件到SD卡中?权限集?问题在下载网络中1K的文件时要假死?(Mars的代码有问题吗)
- 我对linux理解之i2c一
- HOJ2015//Crossed ladders
- python 多线程编程
- 实时监控文本框内容的方法
- java网络编程