Python基础
- Python基础-PEP8: Python代码风格指南
- Python基础-面向对象(四)
- Python基础-面向对象(三)
- Python基础-面向对象(二)
- Python基础-面向对象(一)
- Python基础-模块
- Python基础-异常的捕获和处理
- Python基础-文件夹的操作
- Python基础-文件的操作
- Python基础-函数(三)
- Python基础-函数(二)
- Python基础-函数(一)
- Python基础-字符串,元组,列表,字典,集合之间的相互转
- Python基础-字符串,元组,数组,字典-公共方法
- Python基础-集合set
- Python基础-字典
- Python基础-元组
- Python基础-列表
- Python基础-字符串
- Python基础-break和continue
- Python基础-for循环
- Python基础-while循环
- Python基础-if判断语句
- Python基础-数据类型转换,ASCII码对照表
- Python基础-运算符
- Python基础-输出print和输入input
- Python基础-标识符和关键字
- Python基础-变量以及类型
- Python基础-注释的引入
- Python基础-第一个python程序
- Python基础-简介
Python基础-文件的操作(打开,读,写,重命名,删除等)
文件的操作
文件的操作(打开,读,写,重命名,删除等)
1,打开文件(open)
open('文件名','r') : 只读方式打开文件
open('文件名','r+') : 以读写方式打开,文件不存在报错
open('文件名','w') : 可写方式打开文件
open('文件名','w+') : 以读写方式打开,文件不存在新建
open('文件名','a') : 以追加写方式打开,文件不存在新建
open('文件名','a+') : 以追加读写方式打开,文件不存在新建
open('文件名','rb') : 以二进制读方式打开,只能读文件 , 如果文件不存在,会发生异常
open('文件名','wb') : 以二进制写方式打开,只能写文件 , 如果文件不存在则创建
fr = open('test.txt','r') # 只读 fr_ = open('test.txt','r+') # 读写,不创建 fw = open('test.txt','w') # 只写,创建 fw_ = open('test.txt','w+') # 读写,创建 fa = open('test.txt','a') # 追加写,创建 fa_ = open('test.txt','a+') # 追加读写,创建 frb = open('test.txt','rb') # 二进制读,不创建 fwb = open('test.txt','wb') # 二进制写,创建
2,写数据(write)
使用write()可以完成向文件写入数据,如果文件不存在那么创建,如果存在那么就先清空,然后写入数据
f = open('test.txt','w') # 打开test.txt文件,如果没有就新建,w文件可写 f.write('hello world') #写入hello world,覆盖原先的全部数据 f.close() # 关闭文件
3,读数据(read)
使用read(num)可以从文件中读取数据,num表示要从文件中读取的数据的长度(单位是字节),如果没有传入num,那么就表示读取文件中所有的数据
f = open('test.txt','r') # 打开test.txt文件,如果没有就新建,w文件可写 content = f.read(5) # 最多读取5个数据 print(content) # hello print('*'*10) content = f.read() # 从上次读取的位置继续读取剩下的所有的数据 print(content) # world f.close() # 关闭文件
4,读数据(readlines)
就像read没有参数时一样,readlines可以按照行的方式把整个文件中的内容进行一次性读取,并且返回的是一个列表,其中每一行的数据为一个元素
f = open('test.txt','w') f.write('hello world\nhello world\nhello world\nhello world\nhello world') # 写入5行hello world f = open('test.txt', 'r') content = f.readlines() print(type(content)) print(content) i=1 for con in content: print("%d:%s" % (i, con)) i += 1 f.close() ''' 运行结果 <class 'list'> ['hello world\n', 'hello world\n', 'hello world\n', 'hello world\n', 'hello world'] 1:hello world 2:hello world 3:hello world 4:hello world 5:hello world '''
4,文件重命名
os模块中的rename()可以完成对文件的重命名操作
import os os.rename("test.txt", "test2.txt")
5,删除文件
os模块中的remove()可以完成对文件的删除操作
import os os.remove("test2.txt")
常见问题-文件有内容,却读不出来,如下例子:
f = open('test.txt','w+') f.write('hello') con = f.read() print(type(con)) # <class 'str'> print(con) # con是一个空字符串
按照逻辑,应该读出内容是hello,那为什么读出来是一个空字符串呢?
原因指针问题,文件写入后指针指向末尾[EOF],因此读出空。
2种方法解决此问题:
1,调用close后重新打开,将指针位重置于开头。(不能用含写的方式打开)
f = open('test.txt','w+') f.write('hello') f.close() # 关闭 f = open('test.txt','r') con = f.read() f.close() print(type(con)) # <class 'str'> print(con) # con是的内容为hello
2,调用seek指向开头
f = open('test.txt','w+') f.write('hello') f.seek(0) con = f.read() f.close() print(type(con)) # <class 'str'> print(con) # con是的内容为hello
seek函数:seek(offset[, whence]) ,offset是相对于某个位置的偏移量。位置由whence决定,默认whence=0,从开头起;whence=1,从当前位置算起;whence=2相对于文件末尾移动,通常offset取负值。
记得关闭文件close
当我们操作完文件之后,记得一定要close文件,这是为什么呢?
当我们写文件时,操作系统往往不会立刻把数据写入磁盘,而是放到内存缓存起来,空闲的时候再慢慢写入。只有调用close()方法时,操作系统才保证把没有写入的数据全部写入磁盘。忘记调用close()的后果是数据可能只写了一部分到磁盘,剩下的丢失了。
所以,为了避免忘记close而文件内容又能完全写入,我们可以用with ... as ...,如下:
with open('test.txt','w+') as f: f.write('hello') f.seek(0) con = f.read() print(type(con)) # <class 'str'> print(con) # con是的内容为hello
当我们完成了一系列的文件操作之后,系统会自动完整的将文件数据写入磁盘并关闭文件
文件操作的综合应用:复制文件
要求,新建一个用户输入名字的文件,在文件里面写入内容,然后复制文件
old_file_name = input('请输入文件名:') old_file = open(old_file_name,'w') old_file.write('1,hello world\n2,hello world\n3,hello world') old_file.close() file_flag_num = old_file_name.rfind('.') if file_flag_num > 0: file_name = old_file_name[:file_flag_num] file_flag = old_file_name[file_flag_num:] new_file_name = file_name + '附件' + file_flag else: new_file_name = None new_file = open(new_file_name,'w') old_file = open(old_file_name,'r') # 要重新打开一次,不然是读取的没有内容的文件 contents = old_file.readlines() for con in contents: new_file.write(con) # 关闭文件 old_file.close() new_file.close()
最后修改:2020年2月28日 13:03