python basics of file operations 11-

file

Decoration, a decoration function or class method.

1. The basic operation of the file

open a file:

Note that the absolute path and relative path.

path = 'text.txt'

path = r'/home/pyvip/py_case/text.txt'

file = open (path, 'w +') r read-only representatives

Reads off, all parameters quoted .w overwrite file .a appended at the end.

 

file.read () to read out all the files.

FILE.readline () read line by line.

file.readlines () returns a list, each element row.

file.close () file open operation after completion must pay attention to close.

Write:

file.write('Python')

file.writelents(['1','2','3'])

View and move the file pointer:

file.tell()

file.seek (0,0) the pointer

file.flush () does not turn off to save

file.writelines ([ 'hello', 'python', 'hello', 'world']) will write the incoming list spliced ​​together.

file.tell () See pointer position.

file.seek () moves the pointer position, in accordance with the steps to move bytes.

file.seek (0), to the beginning of the file.

file.readline()

() A row element readlines

File Mode: r: opened in read-only mode.

rb: Open a binary file for read-only, generally used for non-text files such as pictures.

r +: open a file for reading and writing.

rb +: open a file for reading and writing binary format.

w: Open a file for writing if the file exists cover, if there is no new file is created.

wb: opened in binary format only for writing a file, if the file already exists it will be overwritten if the file does not exist, create a new file.

w +: open a file for reading and writing the other with w..

wb: open a file for reading and writing the other binary format with w..

a: Open a file already exists for additional pointer put the end, does not exist to create a new file.

ab: open a file in binary format for additional.

a +: open a file for reading and writing, append mode.

ab +: Open a binary format file for append

2. Context Manager

with open(path,'r') as f:

print(f.read())

with automatically closing the file, the method does not need to perform close

The principle: - enter - () and - exit - ()

open operation is a class

import time

class Runtime:

def --enter--(self):

self.start_time.time()

return self.start_time

def --exit--(self, exc_type, exc_val, exc_tb):

self.ent_time = time.time()

self.run_time = self.ent_time - self.start_time

print(self.run_time)

 

with Runtime () as a: add # with later examples.

Print (A) #a is is the start time, - enter - () Returns the value is assigned to.

for i in range(10000000):

pass

The context management can be easily achieved by these two methods

with --enter-- will return after the value of the variable is assigned as.

After all the code used with open (,) as file:

3.I / O stream

with poen('aaa,txt','r') as file:

print(file.read())

Creating IO operations:

I import

= io.StringIO SiO () # create an object to hold reading.

Write:

sio.write(str(i)) #写在内存当中的,临时储存的.只能写字符串,二进制的字节文件.

读取:

sio.getvalue() #获取值

StringIO在内存中如同打开文件一样操作字符串,因此也有文件的许多方法,当创建的StringIO调用close()方法时,在内存中的数据会被丢失.

BytesIO

创建BytesIO:

import io

bio = io.BytesIO()

写入:

bio.write(b'abcd) #b表示以字节的格式写入.

读取:

sio.getvalue()

BytesIO和StringIO类似,但是BytesIO操作的是Bytes数据.

4.常用工具

os模块提供python和操作系统交互的接口:

os模块自动识别系统类型.

直接调用系统命令:

import os

os.systerm('ls')

通用路径操作:

os.path

os.path.join(r'/home/pyvip',r'pycase')路径拼接,自动加 / .

文件目录操作:

os.mkdir('test') #创建文件夹

os.rename('text','test1')

os提供了python和操作系统交互方式,只要是和操作系统相关,就可以尝试在os模块中找方法.

shutil高级文件操作

shutil模块提供了许多关于文件和文件集合的高级操作:

移动文件: shutil.move('aaa.txt','llll') #文件名,文件夹名.

复制文件: shutil.copytree()

删除文件夹: shutil.rmtree()

 

Guess you like

Origin www.cnblogs.com/winfun/p/10983844.html