Completely solve the python for various files (music, video screen, etc.) to read and write operations

In doing reptiles before when there has been no systematic sort out, save the file.

This time, do a little comb.

1. file read and write operations:

We do not use the current Python file operations, we need to clarify our ideas: first create a file, write to, save and close, which is written to the file of our operations.
Then the file python process requires the use of open () This method, following which we will begin introducing this method.

We first need to know Python to distinguish between binary and text mode open file

The most important in the open () method two parameters file (file name) mode (mode)

Function open () method: to open a file, and returns the object, after using this method, be sure to close the object file (IO operations usually have the cache, do not close the handle, the document is still cached).

In the source code, we can see all of the parameters to be passed to this method

def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True)


file:文件的路径 ,os.path.abspath()可以确定路径(必需)
mode: 文件打开的模式。
buffering:设置缓冲
encoding:字符编码
errors: 是一个可选的字符串,它指定如何编码错误,这个参数不应该在二进制模式中使用。通过如果存在编码错误,则引发ValueError异常(缺省的None具有相同的效果),或者将“ignore”传递给ignore错误。(注意忽略编码错误会导致数据丢失。)
newline:用于区别换行符,它只适用于文本模式
closefd:传入的file参数类型

Distinguish between binary and text, it will depend on the parameters mode

'r'       open for reading (default)
'w'       open for writing, truncating the file first
'x'       create a new file and open it for writing
'a'       open for writing, appending to the end of the file if it exists
'b'       binary mode
't'       text mode (default)
'+'       open a disk file for updating (reading and writing)
'U'       universal newline mode (deprecated)

Of course, a single use these parameters, we want to be able to fulfill the operation, look at the combination of meaning

'r+'            打开一个文件用于读写,文件指针将会放在文件的开头
'rb'            以二进制格式打开一个文件只读。文件指针放在文件的开头
'rb+'           以二进制格式打开一个文件用于读写,文件指针将会放在文件的开头
'w+'            打开一个文件用于读写。
'wb'            以二进制格式打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件,在数据库操作中要慎用w相关的操作
'wb+'           以二进制格式打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。
'a+'            打开一个文件用于读写。
'ab'            以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
'ab+'           以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。          

After the model is created, we started to operate the object file.

The following look at the common method of file objects:

When this file under the operation we can first determine that the file can not read and write

f = open('美文.txt')
print(f.writeable()) #检测这个文件是否可以写入
print(f.readable())  #检测这个文件是否可以读

#在我们遇到 (.*?)able() 这类方法时基本上就是判断能不能执行的功能。

After the test finished, we can use the built-in print function method, the content to be printed, passed the file handle to print. Such is also a method of writing.

f = open('美文.txt','w')
print('hello world',file='f')
f.close()
f = open('美文.txt','a')
f.write('hello world \nhello world \nhello world')     #将字符串写入文件,写入操作
f.close()
f = open('美文.txt','r')                               #读取操作

-------------------------->
print(f.read(100))                                     #从文件读取指定的字节数,如果未给定或为负则读取所有。
-------------------------->
print(f.readline())                                    #读取文件的第一行数据,包括 "\n" 字符
-------------------------->
print(f.readline(3))                                   #读取一行指定的字节数。
-------------------------->
print(f.readlines())                                   #读取所有行并返回列表,若给定sizeint>0,返回总和大约为sizeint字节的行, 实际读取值可能比 sizeint 较大, 因为需要填充缓冲区。

f.close()

-------------------------->
hello world 
hello world 
hello world
-------------------------->
hello world 
-------------------------->
hel
-------------------------->
['hello world \n', 'hello world \n', 'hello world']

The above is an operation on a file, the following few examples, about the pictures, music and video screen, these data are from a binary file. We did not talk much less on the code

2. Download the picture:

import requests
img_data = requests.get('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582812072288&di=e224270da441f72559c487ad90b15442&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201803%2F18%2F20180318210814_fhceh.jpg')
content = img_data.content
print(content)
new_img = open('点赞.jpg','wb')
new_img.write(content)
new_img.close()

3. Save the Music:

music = open('D:/CloudMusic/棉子 - 勇气.ncm','rb')
music_data = music.read()

new_music = open('勇气.ncm','wb')
new_music.write(music_data)
new_music.close()

4. Screen download and save:

import requests
data = requests.get('https://vdept.bdstatic.com/62637150477a6b646432457a384e3938/5575365071497747/fd6bc7e486462424fe27f67126794674a41da7e564332979bf2270458bb48bb62e5df1671132e68472d1428f0292201a.mp4?auth_key=1582801995-0-0-2dfa9e3c1ef90ddf91dc9b208f3a2507')
content = data.content
print(content)
video = open('demo.mp4','wb')
video.write(content)
video.close()


For today this was.

see you again !!!

发布了60 篇原创文章 · 获赞 39 · 访问量 3756

Guess you like

Origin blog.csdn.net/qq_42992704/article/details/104540698