天天看点

学习知多少(python篇):文件方法

作者:LearningYard学苑

学习知多少(python篇):文件方法

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是LearningYard学苑。今天小编为大家带来“学习知多少(python篇):文件方法”,欢迎您的访问。

Share interests, spread happiness, increase knowledge, and leave a good legacy!

Dear you, this is The LearningYard Academy. Today Xiaobian brings you "Learn how much to know (python):File method”, welcome your visit.

在日常生活和工作中,我们常常需要多次使用一个文件,避免繁琐,我们可以借助python文件操作功能实现简化。文件操作的作⽤就是把⼀些内容(数据)存储存放起来,可以让程序下⼀次执⾏的时候直接使⽤,⽽不必重新制作⼀份,省时省⼒。Python中有几个内置模块和方法来处理文件,而使用Python对文件进行读和写是十分简单的。

In daily life and work, we often need to use a file multiple times, to avoid tediousness, we can use the Python file operation function to achieve simplification. The function of file operation is to store some content (data), so that the program can be used directly the next time it is executed, without having to make a new copy, saving time and effort. Python has several built-in modules and methods for working with files, and reading and writing files using Python is very simple.

1、open() 方法

Python open() 方法用于打开一个文件,并返回文件对象。在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出异常。

1. Open() method

The Python open() method is used to open a file and return a file object. This function is required to process the file, and if the file cannot be opened, an exception will be thrown.

注意:使用 open() 方法一定要保证关闭文件对象,即调用 close() 方法。

Note: When using the open() method, be sure to close the file object, i.e. call the close() method.

open() 函数常用形式是接收两个参数:文件名(file)和模式(mode)。

The open() function usually takes two arguments: file and mode.

(1) mode 的分类

t:文本模式 (默认)

x:写模式,新建一个文件,如果该文件已存在则会报错

b:二进制模式

+:打开一个文件进行更新(可读可写)

r:以只读方式打开文件。文件的指针将会放在文件的开头

w:打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件

a:打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾

ab:以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾

ab+:以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾

(1) Classification of modes

t: Text mode (default)

x: Write mode, create a new file, if the file already exists, an error will be reported

b: Binary mode

+: Open a file to update (readable and writable)

r: Open the file as read-only. The pointer to the file will be placed at the beginning of the file

w: Opens a file for writing only. If the file already exists, open the file and edit from the beginning, i.e. the original content is deleted. If the file does not exist, create a new file

a: Open a file for appending. If the file already exists, the file pointer is placed at the end of the file

ab: Opens a file in binary format for appending. If the file already exists, the file pointer is placed at the end of the file

ab+: Opens a file in binary format for appending. If the file already exists, the file pointer is placed at the end of the file

2、file 对象

file 对象使用 open 函数来创建,下面列出了 file 对象常用的函数:

2. File object

The file object is created using the open function, and the functions commonly used by the file object are listed below:

file.close()

关闭文件。关闭后文件不能再进行读写操作。

file.flush()

刷新文件内部缓冲,直接把内部缓冲区的数据立刻写入文件, 而不是被动的等待输出缓冲区写入。

file.next()

Python 3 中的 File 对象不支持 next() 方法。

返回文件下一行。

file.read([size])

从文件读取指定的字节数,如果未给定或为负则读取所有。

file.readlines([sizeint])

读取所有行并返回列表,若给定sizeint>0,返回总和大约为sizeint字节的行, 实际读取值可能比 sizeint 较大, 因为需要填充缓冲区。

file.tell()

返回文件当前位置。

file.write(str)

将字符串写入文件,返回的是写入的字符长度。

file.close()

Close the file. After closing, the file can no longer be read or written.

file.flush()

Flush the internal buffer of the file, and directly write the data of the internal buffer to the file immediately, instead of passively waiting for the output buffer to be written.

file.next()

The File object in Python 3 does not support the next() method.

Returns the next line of the file.

file.read([size])

Reads the specified number of bytes from the file, or all if not given or negative.

file.readlines([sizeint])

Read all rows and return a list, if a sizeint is given >0, return rows with a sum of approximately sizeint bytes, the actual read value may be larger than sizeint because the buffer needs to be filled.

file.tell()

Returns the current location of the file.

file.write(str)

Writes a string to a file, returning the length of the characters written.

3、文件和文件夹操作

在Python中⽂件和⽂件夹的操作要借助os模块⾥⾯的相关功能,具体步骤如下:

1、导入os模块(import os)

2、使用os模块里的函数(os.函数名())

1)文件重命名

os.rename(⽬标⽂件名, 新⽂件名)

2)删除文件

os.remove(⽬标⽂件名)

3)创建文件夹

os.mkdir(⽂件夹名字)

4)删除文件夹

os.rmdir(⽂件夹名字)

5)获取当前目录

os.getcwd()

6)改变默认目录

os.chdir(⽬录)

7)获取目录列表

os.listdir(⽬录)

3. File and folder operations

The operation of Python Chinese and folders should use the relevant functions in the os module, and the specific steps are as follows:

1. Import OS module

2. Use the function in the OS module (OS. function name ())

1) File renaming

os.rename (target filename, new filename)

2) Delete the file

os.remove (destination file name)

3) Create a folder

os.mkdir (folder name)

4) Delete the folder

os.rmdir (folder name)

5) Get the current directory

os.getcwd()

6) Change the default directory

os.chdir (directory)

7) Get a directory listing

os.listdir (directory)

今天的分享就到这里了。

如果您对今天的文章有独特的想法,

欢迎给我们留言,

让我们相约明天。

祝您今天过得开心快乐!

That's all for today's sharing.

If you have a unique idea for today’s article,

please leave us a message,

and let us meet tomorrow.

I wish you a happy day !

本文由learningyard新学苑原创,如有侵权,请联系我们!

翻译来源于谷歌翻译

部分来源于

百度文库

清华大学出版 董付国《Python程序设计基础》

编辑&排版|百味

审核|闫庆红