python-文件操作(1)

 2023-09-10 阅读 30 评论 0

摘要:本文内容涉及python打开/创建文件对象,文件的读写、文件指针位置的移动、获取命令行参数。1. open() open函数以指定模式返回一个file对象,如: file_object = open(filename,access_mode=’r’,buffering=-1),默认是以r模式打开文件。filen

本文内容涉及python打开/创建文件对象,文件的读写、文件指针位置的移动、获取命令行参数。

1. open()
open函数以指定模式返回一个file对象,如: file_object = open(filename,access_mode=’r’,buffering=-1),默认是以r模式打开文件。

filename:表示要打开文件名(字符串),可以是绝对路径或相对路径
access_mode:文件打开的模式(字符串), 常用的模式有’r’,’w’,’a’,不是很常用的还有’u’和’b’
‘r’模式:以读方式打开,不能进行写操作,文件必须是已经存在的
‘r+’模式:以读写方式打开,文件必须是已经存在的
‘w’模式:以写方式打开,不能进行读操作,若文件存在,则先清空,然后重新创建;若不存在,则创建文件
‘w+’模式:以读写方式打开,若文件存在,则先清空,然后重新创建;若不存在,则创建文件
‘a’模式:以追加方式打开,不能进行读操作,把数据追加到文件的末尾;若不存在,则创建文件
‘a+’模式:以读写方式打开,把数据追加到文件的末尾;若不存在,则创建文件
‘b’模式:以二进制模式打开,不能作为第一个字符出现,需跟以上模式组合使用,如’rb’,’rb+’等,
‘u’模式:表示通用换行符支持,文件必须是已经存在的

python创建文件。buffering:表示访问文件采用的缓冲方式,0表示不缓冲,1表示缓冲一行数据,其他大于1的值表示使用给定值作为缓冲区大小,负数表示使用系统默认缓冲机制,默认是-1,一般使用系统默认方式。

2. file()
file是一个类,file()以指定模式创建一个file对象,跟open()具有相同的功能,可以任意替换。一般使用open来创建一个file对象,使用isinstance(obj,file)来判断obj是否是一个文件对象。

3.read()、readline()、readlines()
read():读取指定数目个字节到字符串中,负数将读取至文件末尾,默认是-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>>> file_obj = open('test.txt','r')
>>> file_obj.read()
'dfdff\n'
>>> file_obj.seek(0)
>>> file_obj.read(0)
''
>>> file_obj.read(1)
'd'
>>> file_obj.read(2)
'fd'
>>> file_obj.read(-1)
'ff\n'
>>> file_obj.seek(0)
>>> file_obj.read(1000)
'dfdff\n'
>>> file_obj.seek(0)
>>> file_obj.read(-5)
'dfdff\n'

readline():读取文件的一行,包括行结束符,可以制定size参数的值,默认是-1

1
2
3
4
5
6
7
>>> file_obj = open('test.txt','r')
>>> file_obj.readline()
'dfdff\n'
>>> file_obj.readline(2)
'al'
>>> file_obj.readline(-1)
'exzhou\n'

python怎么打开文件?readlines():读取所有剩余的行,然后作为一个字符串列表返回

1
2
3
>>> file_obj.seek(0)
>>> file_obj.readlines()
['dfdff\n', 'alexzhou\n', 'zhoujianghai\n']

4. write()、writelines()
ps:这两个方法都不会自动加上行结束符,需在写入数据前自己加上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> file_obj = open('test.txt','w+')
>>> file_obj.write('alexzhou')
>>> file_obj.write(' python')
>>> file_obj.seek(0)
>>> file_obj.readline()
'alexzhou python'
>>> l = ['my','name','is','zhoujianghai']
>>> l = ' '.join(l)
>>> file_obj.writelines(l)
>>> file_obj.seek(0)
>>> file_obj.readline()
'alexzhou pythonmy name is zhoujianghai'
>>> file_obj.write('hello \n')
>>> file_obj.write('world \n')
>>> file_obj.seek(0)
>>> file_obj.readline()
'alexzhou pythonmy name is zhoujianghaihello \n'
>>> file_obj.readline()
'world \n'

5. seek()、tell()
seek():移动文件指针到不同的位置,可以指定偏移量和起始位置。起始位置0表示从文件头开始,1表示从当前位置开始,2表示从文件尾开始,默认是0.
tell():表示当前文件指针在文件中的位置,从文件头算起。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>>> file_obj.seek(0)
>>> file_obj.tell()
0
>>> file_obj.seek(5)
>>> file_obj.tell()
5
>>> file_obj.seek(5,1)
>>> file_obj.tell()
10
>>> file_obj.seek(5,2)
>>> file_obj.tell()
57
>>> file_obj.seek(5,0)
>>> file_obj.tell()
5

6. 文件迭代和关闭文件
可以使用for循环一行一行读取文件

1
2
3
4
5
6
7
8
9
10
>>> file_obj = open('test.txt','w+')
>>> file_obj.write('hello \n')
>>> file_obj.write('world \n')
>>> file_obj.seek(0)
>>> for eachline in file_obj:
...     print eachline,
...
hello
world
>>> file_obj.close()

python编程。ps:print后面加一个分号的作用:避免print语句默认在打印的内容后面加一个换行符号。

7. os模块常用属性
由于各操作系统的行分隔符和文件分隔符不一样,所以可以使用os模块的以下属性避免代码移植时碰到这些问题。

os.linesep 行分隔符字符串
os.sep 文件分隔符字符串
os.pathsep 路径分隔符字符串
os.curdir 当前目录字符串
os.pardir 父目录字符串

看下面的打印结果

1
2
3
4
5
6
7
8
9
10
11
>>> import os
>>> os.sep
'/'
>>> os.linesep
'\n'
>>> os.pathsep
':'
>>> os.curdir
'.'
>>> os.pardir
'..'

Python文件。8. 获取命令行参数
创建argv.py文件,输入下面代码

1
2
3
4
import sys
commands = sys.argv
print commands

执行:pyton argv.py 123,打印结果:
[‘argv.py’, ‘123’]


来源: http://codingnow.cn/language/372.html


来自为知笔记(Wiz)


转载于:https://www.cnblogs.com/pangguoping/p/5573965.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/37743.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息