python file operation
file.open(name[,mode[,buffering]])
模式的类型有:
r 默认只读
w 以写方式打开,如果文件不存在则会先创建,如果文件存在则先把文件内容清空(truncate the file first)
a 以追加模式打开 (从 EOF 开始, 必要时创建新文件)用seek也无用。打开的文件也是不能读的。
r+ 以读写模式打开,如果文件不存在则报错,文件可读可写,可写到文件的任何位置
w+ 以读写模式打开 (参见 w ),和r+不同的是,它会truncate the file first
a+ 以读写模式打开 (参见 a ),和r+不同的是,它只能写到文件末尾
rb 以二进制读模式打开
wb 以二进制写模式打开 (参见 w )
ab 以二进制追加模式打开 (参见 a )
rb+ 以二进制读写模式打开 (参见 r+ )
wb+ 以二进制读写模式打开 (参见 w+ )
ab+ 以二进制读写模式打开 (参见 a+ )
file.read([size]) size未指定则返回整个文件,如果文件大小>2倍内存则有问题.f.read()读到文件尾时返回""(空字串)
file.readline( ):表示逐行读取,返回字符串
file.readlines(): 读取所有行,返回字符串列表
file.readline([size]) 返回包含size行的列表,size 未指定则返回全部行
file.write() 接收字符串并且写入到文件中
file.writelines() 接收一个字符串列表作为参数,将他们写入到文件中,换行符不会自动的加入,因此,需要显式的加入换行符
file.tell() 返回一个整数,表示当前文件指针的位置
file.seek(偏移量,[起始位置])
用来移动文件指针
偏移量:单位:比特,可正可负
起始位置:
0-文件头,默认值
1-当前位置
2-文件尾
f.truncate() 清空文件
file.close() 关闭文件
for line in f: print line #通过迭代器访问
Data descriptors defined here:
closed
True if the file is closed
encoding
file encoding
errors
Unicode error handler
mode
file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)
name
file name
newlines
end-of-line convention used in this file
softspace
flag indicating that a space needs to be printed; used by print
>>> fo=open("test.txt","r")
>>> fo.name
'test.txt'
>>> fo.mode
'r'
>>> fo.closed
False
>>> fo.encoding
>>> fo.errors
>>> fo.newlines
>>> fo.softspace
0
# -*- coding:utf-8 -*- from sys import argv script,filename=argv txt=open(filename) print "Here's your file %r:" % filename
print txt.read() print "Type the filename again:"
file_again=raw_input(">>") txt_again=open(file_again) print txt_again.read() > python ex15.py ex15_sample.txt
Here's your file 'ex15_sample.txt':
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
Type the filename again:
>>1.txt
test for sys.argv
>>> open("1.txt").read()
'test for sys.argv'
>>> f=open("test.txt","w")
>>> f.write("Hello \n")
>>> f.write("python!")
>>> f.close()
>>> f=open("test.txt","r")
>>> f.read()
'Hello \npython!'
##指针移动
>>> f=open("test.txt","r")
>>> f.read(3)
'Hel'
>>> f.read(3)
'lo '
>>> f.read(3)
'\npy'
>>> f.read(3)
'tho'
>>> f.read(3)
'n!'
>>> f.read(3)
''
>>> f=open("test.txt","r")
>>> f.readline()
'Hello \n'
>>> f.readline()
'python!'
>>> f.readline()
''
>>> f=open("test2.txt","w")
>>> f.writelines(["first line","+\n","second line"])
>>> f.close()
>>> f=open("test2.txt","r")
>>> f.readline()
'first line+\n'
>>> f.readline()
'second line'
>>> f.readline()
''
>>> f=open("test.txt",'a+')
>>> f.readline()
'dsadsa \n'
>>> f=open("test.txt",'w+')
>>> f.truncate()
>>> f.close()
>>> f=open("test.txt",'a+')
>>> f.readline()
''
# -*-coding:utf-8 -*- from sys import argv
from os.path import exists script,from_file,to_file =argv print "Copying from %s to %s" % (from_file,to_file) in_file=open(from_file)
indata=in_file.read() print "The input file is %d bytes long" % len(indata) print "Does the output file exist? %r" % exists(to_file)
print "Ready,hit RETURN to continue,CTRL-C to abort."
raw_input(">>") out_file=open(to_file,"w")
out_file.write(indata) print "Alright, all done." out_file.close()
in_file.close() > python ex17.py test.txt test2.txt
Copying from test.txt to test2.txt
The input file is 14 bytes long
Does the output file exist? False
Ready,hit RETURN to continue,CTRL-C to abort.
>>
Alright, all done.
> python
Enthought Canopy Python 2.7.11 | 64-bit | (default, Jun 11 2016, 11:33:47) [MSC v.1500 64 bit
Type "help", "copyright", "credits" or "license" for more information.
>>> f=open("test2.txt")
>>> f.read()
'dsads\naaa\nddd\n'
>>>
>>> fo=open("test.txt","r")
>>> fo.readline()
'dsads\n'
>>> fo.tell()
7L
>>> fo.seek(0,0)
>>> fo.readline()
'dsads\n'
>>> fo.tell()
7L
>>> fo.seek(8,0)
>>> fo.readline()
'aa\n'
>>> fo.seek(7,0)
>>> fo.readline()
'aaa\n'
>>> fo.seek(0,2)
>>> fo.tell()
17L
>>> fo.seek(-8,2)
>>> fo.tell()
9L
>>> fo.read()
'a\nddd\n'
python file operation的更多相关文章
- Python & file operation mode
Python & file operation mode create/read/write/append mode https://docs.python.org/3/library/fun ...
- Python:文件操作技巧(File operation)(转)
Python:文件操作技巧(File operation) 读写文件 # ! /usr/bin/python # -*- coding: utf8 -*- spath = " D:/dow ...
- Python File I/O
File is a named location on disk to store related information. It is used to permanently store data ...
- File Operation using SHFileOperation
SHFILEOPSTRUCT Original link: http://winapi.freetechsecrets.com/win32/WIN32SHFILEOPSTRUCT.htm Refere ...
- mysql数据库报错:InnoDB: Operating system error number 13 in a file operation
环境:centos6.5 x86_64 启动mysql发现日志报错(日志路径可以查看/etc/my.cnf的配置) 160722 10:34:08 [Note] Found 42570716 of 4 ...
- InnoDB: Operating system error number 87 in a file operation. 错误87的解决方法
InnoDB: Operating system error number 87 in a file operation. 错误87的解决方法 140628 8:10:48 [Note] Plugi ...
- Adobe ZXPInstaller 报错 Installation failed because of a file operation error.
1. Drag a ZXP file or click here to select a file. 拖放一个 zxp 文件或点击打开选择一个 zxp 文件来安装: 2. Installation f ...
- Python - File - 第十八天
Python File(文件) 方法 open() 方法 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OS ...
- [转]python file文件操作--内置对象open
python file文件操作--内置对象open 说明: 1. 函数功能打开一个文件,返回一个文件读写对象,然后可以对文件进行相应读写操作. 2. file参数表示的需要打开文件的相对路径(当前 ...
随机推荐
- Ubuntu11.10 更新软件源source.list (ZT)
添加完列表后执行 sudo apt-get update sudo apt-get upgrade --------添加列表------------------------------------- ...
- Python变量与常量
变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变.基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中.常量是一块只读的内存区域,常量一旦被初始化就不能被 ...
- HDU2073(暴力) VS HDU5214(贪心)
题意:给出n组l[i],r[i],求出能够相互连接的最大个数,比如(1,2) ,(2,3),(5,6)就是可以连接的3组数据: 思路:2073数组大小为100,纯暴力就可以了,不过注意排序时,按照r的 ...
- sqlserver中自定义函数+存储过程实现批量删除
由于项目的需要,很多模块都要实现批量删除的功能.为了方便模块的调用,把批量删除方法写成自定义函数.直接上代码. 自定义函数: ALTER FUNCTION [dbo].[func_SplitById] ...
- WebService是什么?
一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...
- Windows 2003 AD升级Windows 2008
把Windows 2008 R2的光盘放到主域控中,运行 adprep32 /forestPrep adprep32 /domainprep adprep32 /domainprep /gpprep ...
- Newspaper Headline_set(upper_bound)
Description A newspaper is published in Walrusland. Its heading is s1, it consists of lowercase Lati ...
- git pull --rebase 做了什么? 以及 Cannot rebase: You have unstaged changes 解决办法
最近刚学 git rebase,觉得很牛逼的样子, 结果今天就被打脸了. git pull --rebase 报错: Cannot rebase: You have unstaged changes ...
- CentOS安装Nginx负载
一.准备 1.安装Nginx 地址:http://www.cnblogs.com/rainy-shurun/p/4983260.html 二.配置负载 1.配置nginx.conf
- 博客引索 - imsoft.cnblogs
Java C Delphi 日常工具 图片处理 视频处理 系统工具 知识经验 奇思妙想 网站推荐