python 文件的基本操作

	(今天的学习都是基于 GreenFlowerPorcelain.txt的文本文档)
 1 Green Flower Porcelain
 2 You are the face that has changed my whole world.
 3 You are the face that I see everywhere I go.
 4 You are so beautiful to me that I can't explain ,
 5 Just like a green flower porcelain.
 6 You're like a moon that I awaken to say hello,
 7 So beautiful and bright that you make me content to play it so.
 8 I see your face on the leaves,telling me how lonely I have been.
 9 This is a dream of mine that I have just dreamed.
10 Just see your smiling face everywhere I go.
11 The love I feel for you to shine inside me.
12 But it's all over now you're gone.
文件概念

编程中,很多时候都离不开对文件的操作。在人工智能中,很多东西会涉及到数据科学,经常需要我们处理一些数据。这些数据通常会保存在一些文件里,然后,我们运用Python以及相关的数据科学等库生成或者处理这些文件。

在Python中,常用的可以生成或者读取的文件类型有很多,比如txt, csv, xlsx, json, xml,html, mp3, mp4等文件。

文件的打开与关闭
(1) open()

用Python进行文件的读取操作,必须先用Python内置的open()函数打开一个文件,创建一个file对象,相关的方法才可以调用它进行读写。

语法格式如下:

>>> f = open(file_name [,mode][,encoding="utf-8"][,buffering])

各个参数的解释如下:

  • file_object是open()返回的文件对象;
  • file_name:是要访问的—文件名的字符串值。
  • mode:决定了打开文件的模式:只读,写入,追加等。所有可取值见如下的完全列表,这个参数是非强制的,默认文件访问模式为只读®。
  • encoding指定文件编码,一般使用"utf-8"或者"gbk"。
  • buffering:如果buffering的值被设为0,就不会有寄存;如果buffering的值取1,访问文件时会寄存行;如果将buffering的值设为大于1的整数,表明了这就是的寄存区的缓冲大小;如果取负值,寄存区的缓冲大小则为系统默认。

读取txt文本的内容

f = open("文件名.txt","r",encoding="utf-8")
f.read() #阅读
f.close() #关闭
(2) file对象的属性

​ 一个文件被打开后,会得到一个file对象,你可以得到有关该文件的各种信息。

一下是和file对象相关的常用属性的列表:

属性描述
f.name返回文件的名称。
f.mode返回被打开文件的访问模式。
f.closed返回true如果文件已被关闭,否则返回false。返回true如果文件已被关闭,否则返回false。

不同模式打开文件的完全列表:

模式描述
r以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。
rb以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。
r+打开一个文件用于读写。文件指针将会放在文件的开头。
rb+以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。
w打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
w+打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb+以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
a打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
ab以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
a+打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
ab+以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。
(3) close() 函数

​ file对象的 close()方法刷新缓冲区里任何还没写入的信息,并关闭该文件,这之后便不能再进行写入。当一个文件对象的引用被重新指定给另一个文件时,Python 会关闭之前的文件。用 close()方法关闭文件是一个很好的习惯。

因为文件对象会占用计算机操作系统的资源,并且操作系统同一时间能打开的文件数量也是有限的,为节省计算机资源和提高性能,我们需要在使用完文件后,将其关闭。

#### 文件的读写操作

file对象提供了一系列方法,能让我们的文件访问更轻松。来看看如何使用read()和write()方法来读取和写入文件。

##### (1) write()

write()方法可将任何字符串写入一个打开的文件。需要重点注意的是,Python字符串可以是二进制数据,而不是仅仅是文字。write()方法不会在字符串的结尾添加换行符(’\n’):

语法:

f.write(string)

在这里,被传递的参数是要写入到已打开文件的内容。

如:

>>> f = open("TestFile.txt", "w+", encoding="utf-8")
>>> f.write("青花瓷十分好听!")
8
>>> f.close()
>>>

上述方法会创建TestFile.txt文件,并将收到的内容写入该文件,并最终关闭文件。如果你打开这个文件,将看到以下内容:

青花瓷十分好听!

##### (2) read()方法

read()     一次读取全部文件内容。这里,Python字符串可以是二进制数据,而不是仅仅是文字。

语法:

fileObject.read([count])

- 该方法从文件的开头开始读入,传递的count是要从已打开文件中读取的字节计数。
- 如果没有传入count,读取全部文件内容。

例:这里我们用到以上创建的 TestFile.txt 文件。

>>> f = open("GreenFlowerPorcelain.txt", "r")
>>> sr1 = f.read(20)
>>> sr1
' 1 Green Flower Porc'
>>> len(sr1)
20
>>> f = open("GreenFlowerPorcelain.txt", "r")
>>> f.read()

" 1 Green Flower Porcelain\n 2 You are the face that has changed my whole world.\n 3 You are the face that I see everywhere I go.\n 4 You are so beautiful to me that I can't explain ,\n 5 Just like a green flower porce lain.\n 6 You're like a moon that I awaken to say hello,\n 7 So beautiful and bright that you make me content to play it so.\n 8 I see your face on the leaves,telling me how lonely I have been.\n 9 This is a dream of mine that I have just dreamed.\n10 Just see your smiling face everywhere I go.\n11 The love I feel for you to shine inside me.\n12 But it's all over now you're gone."
>>>
(3) readline()方法
  • f.readline() 会从文件中读取单独一行。换行符为’\n’。

    f.readline() 如果会返回一个空字符串,说明已经读取到最后一行。

>>> f = open("GreenFlowerPorcelain.txt","r")
>>> f.readline()
' 1 Green Flower Porcelain\n'
>>> f.readline()
' 2 You are the face that has changed my whole world.\n'
>>> f.readline()
' 3 You are the face that I see everywhere I go.\n'
>>> for i in range(3):
...
>>>
>>> f.readline(2)
' 4'
>>> f.readline(20)
' You are so beautifu'
>>> f.close()
>>>
(4)readlines()方法

​ f.readlines([sizehint])

  • 以列表的形式返回该文件中包含的所有行,列表中的一项表示文件的一行。
  • 如果设置可选参数 sizehint,则读取指定长度的字节,并且将这些字节安行分割。
>>> f = open("GreenFlowerPorcelain.txt","r")
>>> f.readlines()
[' 1 Green Flower Porcelain\n', ' 2 You are the face that has changed my whole world.\n', ' 3 You are the face that I see everywhere I go.\n', " 4 You are so beautiful to me that I can't explain ,\n", ' 5 Just like a green flower porcelain.\n', " 6 You're like a moon that I awaken to say hello,\n", ' 7 So beautiful and bright that you make me content to play it so.\n', ' 8 I see your face on the leaves,telling me how lonely I have been.\n', ' 9 This is a dream of mine that I have just dreamed.\n', '10 Just see your smiling face everywhere I go.\n', '11 The love I feel for you to shine inside me.\n', "12 But it's all over now you're gone."]
>>>

另一种方式是迭代一个文件对象然后读取每行:

>>> f = open("GreenFlowerPorcelain.txt","r")
>>> for line in f:
...     print(line)
...

 1 Green Flower Porcelain

 2 You are the face that has changed my whole world.

 3 You are the face that I see everywhere I go.

 4 You are so beautiful to me that I can't explain ,

 5 Just like a green flower porcelain.

 6 You're like a moon that I awaken to say hello,

 7 So beautiful and bright that you make me content to play it so.

 8 I see your face on the leaves,telling me how lonely I have been.

 9 This is a dream of mine that I have just dreamed.

10 Just see your smiling face everywhere I go.

11 The love I feel for you to shine inside me.

12 But it's all over now you're gone.
>>>
>>> f = open("GreenFlowerPorcelain.txt","r")
>>> for line in f:
...     print(line,end="")
...

 1 Green Flower Porcelain
 2 You are the face that has changed my whole world.
 3 You are the face that I see everywhere I go.
 4 You are so beautiful to me that I can't explain ,
 5 Just like a green flower porcelain.
 6 You're like a moon that I awaken to say hello,
 7 So beautiful and bright that you make me content to play it so.
 8 I see your face on the leaves,telling me how lonely I have been.
 9 This is a dream of mine that I have just dreamed.
10 Just see your smiling face everywhere I go.
11 The love I feel for you to shine inside me.
12 But it's all over now you're gone.>>>
 1 Green Flower Porcelain
| 2 You are the face that has changed my whole world.
| 3 You are the face that I see everywhere I go.
| 4 You are so beautiful to me that I can't explain ,
| 5 Just like a green flower porcelain.
| 6 You're like a moon that I awaken to say hello,
| 7 So beautiful and bright that you make me content to play it so.
| 8 I see your face on the leaves,telling me how lonely I have been.
| 9 This is a dream of mine that I have just dreamed.
|10 Just see your smiling face everywhere I go.
|11 The love I feel for you to shine inside me.
|12 But it's all over now you're gone.|>>>
1 Green Flower Porcelain
2 You are the face that has changed my whole world.
3 You are the face that I see everywhere I go.
4 You are so beautiful to me that I can't explain ,
5 Just like a green flower porcelain.
6 You're like a moon that I awaken to say hello,
7 So beautiful and bright that you make me content to play it so.
8 I see your face on the leaves,telling me how lonely I have been.
9 This is a dream of mine that I have just dreamed.
10 Just see your smiling face everywhere I go.
11 The love I feel for you to shine inside me.
12 But it's all over now you're gone.
>>>

我们在写程序的时候,读写随时可能产生错误,一旦出错,无法调用close()方法,就不能保证在出错时能正确关闭文件。

所以,为了保证无论是否出错都能正确地关闭文件,我们可以用下列方法:

方法1:

>>> with open("GreenFlowerPorcelain.txt","r",encoding="utf-8") as f:
...     print(f.read())
...
 1 Green Flower Porcelain
 2 You are the face that has changed my whole world.
 3 You are the face that I see everywhere I go.
 4 You are so beautiful to me that I can't explain ,
 5 Just like a green flower porcelain.
 6 You're like a moon that I awaken to say hello,
 7 So beautiful and bright that you make me content to play it so.
 8 I see your face on the leaves,telling me how lonely I have been.o 
 9 This is a dream of mine that I have just dreamed.
10 Just see your smiling face everywhere I go.
11 The love I feel for you to shine inside me.
12 But it's all over now you're gone.
>>>

方法2:

>>> with open("GreenFlowerPorcelain.txt","w",encoding="utf-8") as f:
...     f.write('Hello,World')
...
11
>>>

#### 文件定位读取

- f.seek()
  - seek(offset [,from]) 改变当前文件的位置。
    - Offset变量表示要移动的字节数。
    - From变量指定开始移动字节的参考位置。如果from被设为0,这意味着将文件的开头作为移动字节的参考位置。如果设为1,则使用当前的位置作为参考位置。如果它被设为2,那么该文件的末尾将作为参考位置。

f = open(文件名)
f.seek([偏移字节数], 0) # 开头
f.seek([偏移字节数], 1) # 当前操作的位置
f.seek([偏移字节数], 2) # 末尾

- f.tell()  
- tell() 获取文件内的当前位置;换句话说,下一次的读写会发生在文件开头这么多字节之后。

例子:

>>> f = open("GreenFlowerPorcelain.txt")
>>> f.read(30)
' 1 Green Flower Porcelain\n 2 Y'
>>> f.tell()
31
>>> f.seek(32)
32
>>> f.tell()
32
>>> f.read(30)
'u are the face that has change'
>>>
文件操作

Python的os模块提供了执行文件处理操作的方法,比如重命名和删除文件。需使用这个模块,必须先导入它,然后才可以调用相关的各种功能。

  • 文件重命名
# 先创建一个TestFile.txt的文件
>>> import os
>>> os.rename("TestFile.txt","TestFild.md")#第一个参数是已经创建好的文件,第二个参数是改变后缀名为.md的文件
  • 文件删除
>>> import os
>>> os.remove("TestFile.md")
目录操作
  • 文件夹创建
>>> import os
>>> os.mkdir("test_folder")
  • 获取当前目录
>>> import os
>>> os.getcwd()
  • 删除目录
>>> import os
>>> os.rmdir("test")
  • 改变默认目录(指定文件夹)
import os
os.chdir("../")
  • 获取目录列表
import os
os.listdir()
作业1:

GreenFlowerPorcelain.txt 文件读取内容到a.txt文件中

>>> f = open("GreenFlowerPorcelain.txt","r")
>>> ff=f.read()
>>> ff
" 1 Green Flower Porcelain\n 2 You are the face that has changed my whole world.\n 3 You are the face that I see everywhere I go.\n 4 You are so beautiful to me that I can't explain ,\n 5 Just like a green flower porcelain.\n 6 You're like a moon that I awaken to say hello,\n 7 So beautiful and bright that you make me content to play it so.\n 8 I see your face on the leaves,telling me how lonely I have been.\n 9 This is a dream of mine that I have just dreamed.\n10 Just see your smiling face everywhere I go.\n11 The love I feel for you to shine inside me.\n12 But it's all over now you're gone."
>>> fff = open("a.txt","w")
>>> fff.write(ff)
588
>>> fff.close()	#关闭
>>> f.close()	#关闭
作业2:

演示修改文件名称(并不会真的改变文件名称)

>>> name = "tesGreenFlowerPorcelain.txt"
>>> pos = name.rfind('.')
>>> new_name = name[:pos] + '_copy' + name[pos:]
>>> new_name
'tesGreenFlowerPorcelain_copy.txt'
作业2_1:

input演示修改文件名称(并不会真的改变文件名称)

name = input("请输入一个文件名称: ")
pos = name.rfind('.')
new_name = name[:pos] + '_copy' + name[pos:]
print(new_name)
作业3:

input输入 复制一个文件,并重命名

name = input("请输入一个文件名称: ")
pos = name.rfind('.')
new_name = name[:pos] + '_copy' + name[pos:]
# print(new_name)
f1 = open(name,"r",encoding="utf-8")
content = f1.read()
f2 = open(new_name,"w",encoding="utf-8")
f2.write(content)
f1.close()
f2.close()
批量重命名
  • (1)获取要命名的文件夹名字
  • (2)获取指定文件夹中 所有文件的名字
  • (3)重命名

(1)指定默认文件夹

# 首先咱们创建一个文件夹 后在里面创建几个文件 接下来就可以进行下面的操作了
import os

# (1) 获取要命名的文件夹名字
folder_name = input("请输入要重命名的文件夹:")
# (2) 获取指定文夹中 所有文件的名字
files_name = os.listdir(folder_name) # 不加报错

os.chdir(folder_name)
# (3) 重命名
for name in files_name:
	print(name)
    os.rename(name,"[长沙]-"+name)

(2)不指定默认文件夹

# 需求:对文件夹里面的所有文本文档做出重命名(在名字的前面加上Test-)
import os

# (1) 获取要命名的文件名字
folder_name = input("请输入要重命名的文件夹:")
# (2) 获取指定文件夹中 所有文件的名字
files_name = os.listdir(folder_name) # 不加报错

# os.chdir(folder_name)
# (3) 重命名
for name in files_name:
    print(name)
    old_file_name = folder_name + "/" + name
    new_file_name = folder_name + "/" + "Test-" + name
    os.rename(old_file_name,new_filw_name)     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值