Quick Python syntax: 12. The file input and output

Back to Contents

 

 (1) Basic operation file

● File common operations

Built-in function or method description
open(name [,mode [,buffering]]) Built-in functions. Used to open the file and returns a file object (file objects). See below
f.close() object method file, close the file.
f.read() The method of the file object, read up to n bytes, if n specified, the default read the entire file. The contents read out returned as a string.
f.readline(size=-1) file object methods. Reading the entire row and returns a string, if the specified size, the Bank read up to size bytes. Whole row is read out to return a string containing a newline.
f.readlines(hint=-1) file object methods. Read all rows and returns a list of character string of each line is a list of element, most hint number of rows read. (Since the file object can now iterate directly, so the readlines () This method is now used less, detailed description below iterator file.)
f.write(s) file object methods. S given string written to the file. (To wrap, it needs its own line breaks in s plus Python)
f.writelines(lines) file object methods. Writing all the strings in the sequence lines. (Newline ibid)
f.tell() file object methods. This returns the current position of the file object in the file pointer, the return value is an integer.
f.seek(offset [,whence]) file object methods. Search file locations, see below
f.truncate(size=None) file object methods. The truncated to a maximum file size bytes.
f.flush() file object methods. The contents of the output buffer is written to disk and forced to empty the output buffer.
f.fileno() file object methods. Returns an integer file descriptor.
f.isatty() file object methods. Determining whether the file object is an interactive terminal, returns a Boolean value.

 

● file objects common attributes

Attributes Explanation
closed Boolean value that indicates whether the file has been closed.
mode String, the file open mode. See below open () mode function parameters described.
name Strings, file names
newlines Represent a newline character in the file actually encountered said that if not met newline, compared None.
encoding String representing the file encoding. See below open () encoding function parameters described.

 

 

 

● open(name ,mode='r' ,buffering=-1, errors=None, newline=None, closefd=True, opener=None)

Description:

Parameter name : file name with full path

Parameters MODE : in the table below for each character, may be used in combination, such as 'rb' or 'rw' and the like.

Buffering Parameters : default to -1, i.e., disk block size of the buffer size based system (often 4096 bytes), any other number greater than a specified buffer size (in bytes).

Parameter encoding : encoding a file name, such as: 'utf-8', ' ascii', 'cp936' and the like.

Parameter NEWLINE : newline common control behavior patterns, may be set to: '\ n', '\ r', '\ r \ n', '', None like. If set to None, all forms of line breaks (eg: '\ n', '\ r', '\ r \ n') will be converted to '\ n'. If set to '' (the empty string), all forms of wrap will be identified as line breaks, but does not convert the input text;

Parameters closefd : Boolean control when you call the close () method, whether the actual underlying file descriptor is closed, usually the default is True.

character meaning
r Read mode (default)
w Write mode (first file will remove all existing content, and then write it from scratch)
x If the file already exists, use the x mode error
a Additional write mode (start writing existing content of the file from the end)
b Binary mode
t Text mode (default)
+ Read / write mode, with 'r' or 'w' in combination. 'R +' represents a write (but not clear the existing contents); 'w +' represents a write (erase all content)
 

About "Text Mode" and "binary mode" Description:

In Python3, open the file in the text mode, the read operation will return the Unicode string; if the file is opened in binary mode, returns the byte string.

If the data file is a binary file itself, since the return Content Format read () method is a character string, the "text mode" and "binary pattern" read content looks like distortion, it requires further processing.

For text files, use the "Text Mode" and "binary mode" effect is almost the only difference is in the "text mode", Python will automatically file line breaks to make some converts. When reading a text file format, Windows, Windows will form newline '\ r \ n' is converted into a standard form Python newline '\ n'. Similarly, when writing Windows text file, the newline Python '\ n' is converted into the form of a Windows '\ r \ n' written to the file. In the Linux platform without any conversion.

 

Back to index

 

 

● seek(offset [,whence])

Description:

The given offset and whence offset rule, the current location of this file object pointer to an arbitrary position. is an integer offset, the offset The whence indicate the start position of the offset, as follows: (when using the macro name, the module needs to import os

Macro Name Integer value Explanation
os.SEEK_SET 0 offset offset from the beginning of the file.
Osksik_cur 1 offset is the offset of the current position.
os.SEEK_END 2 offset is the offset from the start end of the file, this time offset is usually negative.
 

 

Back to index

 

 

● Make sure the file is normally closed method

Method a: Use try / finally

f = open (filename) # open file 
the try: 
    # ...... file operation 
the finally: 
    f.close ()

 

Two: with a context manager

with open (filename) as wf: # with the file will open its assignment to a variable wf on 
    do_somethin (wf)    
    
# After the above statement block, the file is automatically turned off, even if the end is caused by abnormal as well.

 

 

 

 (2) reads the contents of the file with the file iterator

If the file is not large, traditional file read operation is as follows like this (without the use of file iterators).

将文件内容一次性全部读出,每次处理一个字符:

f = open(filename)
for c in f.read():
    print(c)
f.close()

将文件所有的行一次性全部读出,每次处理一行:

f = open(filename)
for line in f.readlines():
    print(line)
f.close()

若文件很大,则可以每次仅读取一行:

f = openfile(filename)
while True:
    line = f.readline()
    if not line: break    # 当line返回空值时,表面已读到文件尾
    print(line)
f.close()

 

而现在,文件对象本身是支持迭代的。也就是说,对于很大的文件,不用一次性全部读出所有内容,用文件对象迭代器就可以在每次循环时只读出文件的一部分。

文件迭代器的用法:

f = open(filename)
for line in f:
    print(line)
f.close()

也可以让Python自己负责关闭文件:

for line in open(filename):
    print(line)

 

 

● 使用fileinput模块

使用fileinput模块也可以对文件进行迭代,fileinput模块还提供一些其他方法使能够更方便地对多个文件和多个输入输出流操作。

fileinput模块中的常用函数

函数 说明
input(files=None, inplace=False, backup='', mode='r', openhook=None) 新建一个FileInput类的实例,用于对若干文件进行操作。files参数为文件名或输入流列表,若缺省则默认为sys.argv[1:]。若inplace参数为True,文件将原地处理,即:将文件内容复制到backup指定的文件,并将标准输出流重定向到本文件。
filename() 返回当前文件的名称。
fileno() 返回当前打开文件的文件描述符(整数),若未打开文件,则返回-1。
lineno() 返回当前累计的行数。即若处理多个文件,行数会累计。
filelineno() 返回当前文件当前行行号。
isfirstline() 刚才读入的行是否为文件的第一行。
isstdin() 最后读入的一行是否来自sys.stdin
nextfile() 关闭当前文件,移动到下一个文件。
close() 关闭序列。

 

用fileinput对文件进行迭代:

import fileinput
for line in fileinput.input(filename):
    print(line)

 

对目标文件的每行后面添加行号注释:

# test.py
import fileinput

for line in fileinput.input(inplace=True):    # files参数为空,默认使用sys.argv[1:]作为输入流列表
    line = line.rstrip()
    num = fileinput.lineno()
    print('%-40s # %2i' %(line, num))
    
# 运行:python test.py a.txt 后,会在a.txt文件的每一行后添加注释行号

 

 

 

 (3)输入与输出

Python解释器提供了3种标准文件对象:标准输入、标准输出、标准错误,分别在sys模块中表示为:sys.stdin, sys.stdout, sys.stderr。 通常,stdin被映射到接受用户键盘输入信息,而stdout和stderr在屏幕上生成文本。在某些情况下,集成开发环境(IDE)可能会更改sys.stdin, sys.stdout和sys.stderr。

文件的read()和write()也可以用于输出流和输入流,效果与print()和input()内置函数相同。

import sys
sys.stdout.write('Hello')     # 相当于:print('Hello')
a = sys.stdin.readline()      # 相当于:a = input() ,使用stdin在a中会有一个换行符。

Linux中的管道符号(|),将前一个命令的“标准输入”和后一个命令的“标准输出”连在一起,如果你的程序要读取管道中来的数据,只需使用读标准输入即可,如:

import sys
tx = sys.stdin.read()

 

● print()函数

print()函数可接收若干个要在屏幕上打印的参数,入参中间用逗号隔开,在屏幕上各个显示的入参之间以空格分隔。 若要将print()的内容重定向到一个文件,可以使用file=outfile关键字参数。若要更改默认的空格分隔,可使用sep=sepchar关键字参数。

print('Hello', 1, 2, 3)   # 输出显示为:Hello 1 2 3
print('Hello', 1, 2, 3, sep=',')    # 输出显示为:Hello,1,2,3
print('Hello', 1, 2, 3, file=f)     # 输出内容重定向到文件对象f

若要在输出文本中大量插入变量,可使用format()方法或string.Template方法,关于它们的用法 详见这里

 

 

 

 (4)pickle与shelve模块

 

● pickle模块

pickle模块将对象序列化为一个字节流,这个字节流可以写入到文件并在以后从文件读出还原成对象。出于安全考虑,程序不应该处理来自不可信来源的文件来还原成对象。

序列化用户定义类的实例时,只会序列化数据,不会保存相应的类定义,序列化后的数据只包含相关类和模块的“名称”。且在还原一个实例时,不会调用其类方法__init__()。

pickle模块比较适合用于序列化:数字、字符串、元组、列表、只包含可序列化对象的字典、在模块顶层定义的用户定义类的实例。

pickle常用方法

函数 说明
pickle.dump(obj, file, protocol=None) 将obj序列化并存储到“文件对象”file中。protocol可为:0、1、2、3、pickle.HIGHEST_PROTOCOL(自动选择最新协议)。若obj不支持序列号,则引发pickle.PicklingError异常
pickle.dumps(>obj, protocol=None) 与上面的dump()功能相同,只是不将序列化后的对象写入文件,而是将其作为一个bytes对象返回。
pickle.load(file) 从“文件对象”file中加载并还原一个序列化对象。如果该文件无法还原,则引发pickle.UnpicklingError异常;如果检测到文件尾,则引发EOFError异常。
pickle.loads(bytes_obj) 与上面load()功能相同,但不是从文件对象还原对象,而是从入参的bytes对象还原。

 

序列化对象和还原序列化示例:

# 序列化写入文件:
obj1 = SomeObject()
obj2 = SomeObject()
f = open(filename, 'wb')
pickle.dump(obj1, f)      # 先写入obj1
pickle.dump(obj2, f)      # 再写入obj2
f.close()

# 还原序列化对象:
f = open(filename, 'rb')
o1 = pickle.load(f)
o2 = pickle.load(f)
f.close()

 

 

● shelve模块

shelve模块是基于pickle模块建立的,但比pickle模块更加自动化。模块中的open()函数会创建一个Shelve对象,可以当做一个普通字典来使用(键名必须是字符串),在调用其sync()或close()方法后,其内容将被自动写入文件。

shelve使用示例:

import shelve
# 保存到文件:
s = shelve.open('test')    # shelve模块会自动添加扩展名.dat
s['a'] = [1,2,3]
s.close()                  # 保存内容到文件

# 从文件中读出:
s = shelve.open('test')
print(s['a'])
s['b'] = 'abc'             # 追加内容
del s['a']                 # 删除键'a'及其内容
s.sync()                   # 强制保存内容到文件
s.close()                  # close()会自动调用sync()方法而实现保存作用。

 

 

 

 (5)shutil模块

shutil模块用于执行高级的普通文件操作,例如:复制、移动、重命名等,但并不能处理管道、块设备等特殊文件。

函数 说明
shutil.copy(src, dst) 将文件src复制到文件或目录下(若dst为目录,则复制到目录下;若dst为一文件名,则复制到dst同级目录下并重命名为指定文件名)。返回新复制的文件名(含路径)
shutil.copy2(src, dst) 与上面的copy()类似,但同时复制了最后访问时间和修改时间。
shutil.copytree(src, dst, symlinks=False, ignore=None) 递归地复制src下的整个目录树,创建目标目录dst(且不应该已存在),并使用copy2()复制每个文件。如果symlinks为True,则对于源目录树中的链接文件仅作为链接文件复制,否则复制真实文件到新目录树。 如果在复制过程中发生错误,则将收集错误,处理结束时提示Error异常,异常的参数是一份包含了所有错误的元组列表 (srcname, dstname, exception)。
shutil.move(src, dst) 将文件或目录从src移动到dst。如果src移动到了不同的文件系统中,将递归地复制src。
shutil.rmtree(path, ignore_errors=False, onerror=None) 删除整个目录树。如果ignore_errors为真,错误将被忽略;否则错误由onerror函数处理。
shutil.copymode(src, dst) 将文件的模式权限位(如:777)从src复制到dst
shutil.copystat(src, dst) 将权限位、最后访问时间、最后修改时间从src复制到dst。文件内容、拥有人等保持不变。
shutil.copyfile(src, dst) 将src的内容全部读出后,复制到dst。
shutil.copyfileobj(f1, f2 [,length]) 将打开的文件对象f1中的所有内容复制到打开的文件对象f2,length可指定最大缓冲容量,若位负数,则一次操作中复制全部数据。
shutil.ignore_pattern(*patterns) 创建一个函数,忽略所有patterns中给出的通配符样式模式。返回函数的主要用途是作为shutil.copytree()函数的ignore参数,也可给os.walk()函数使用。

 

 

 

 (6)os模块中的常用文件操作

● os模块

函数/属性 说明
sep 属性。操作系统用于分隔路径各个部分的字符,在Linux上为'/',在Windows上为'\',在Mac上为':'
getcwd() 返回当前进程的工作路径字符串。
chdir(path) 将当前进程的工作目录修改为path。
listdir(path) 返回包含目录路径中各文件名和子目录名的列表(不包括'.'和'..'),不递归搜索。
mkdir(path [,mode]) 创建模式为mode的目录,默认模式为777。
makedirs(path [,mode]) 在创建目录时,会同时创建包含叶子目录所需要的中间级目录,若叶子目录已存在或者无法创建,将引发OSError异常。
rmdir(path) 删除目录,目录必须为空。
removedirs(path) 递归地目录删除函数,即在path描述链上的所有目录都将被删除。若其中某个目录不为空,则停止继续向上删除目录。
remove(path) 删除文件,同unlink()函数。
unlink(path) 删除文件,同remove()函数
rename(src, dst) 将文件或目录src重命名为dst
renames(old, new) 它会尝试创建新路径所需的中间目录,重命名完成之后,将使用removedirs()删除旧名称的目录链。

 

os.path模块

os.path模块以一种可移植的方式操作路径名称,可由os模块导入。

函数 说明
文件与目录的路径操作
exists(path) 如果path指定的文件或目录存在,则返回True。若path是已损坏的符号链接,则返回False。
lexists(path) 功能同上,但只要链接文件存在,即便链接损坏也返回True。
isdir(path) 如果path是目录则返回True。
isfile(path) 如果path是文件则返回True。
islink(path) 如果path是符号链接则返回True。
ismount(path) 如果path是挂载点则返回True。
文件与目录的属性操作
getsize(path) 返回path的大小,以字节为单位
getatime(path) 返回最后一次访问path的时间,返回值是从纪元起始点开始的秒数。
getctime(path) 返回创建path的时间(Windows)或最后一次修改path的时间(Linux),返回值是从纪元起始点开始的秒数。
getmtime(path) 返回最后一次修改path的时间,返回值是从纪元起始点开始的秒数。
samefile(path1, path2) 如果path1和path2引用同一个文件或目录,返回True。
sameopenfile(fp1, fp2) 如果打开的文件对象fp1和fp2引用同一个文件,则返回True。
samestat(stat1, stat2) 如果fstat()、lstat()或stat()饭hi的stat对象stat1和stat2引用同一个文件,则返回True。
以下为纯路径字符串操作(不管实际目录或文件是否存在)
dirname(path) 返回path所在目录的名称。
basename(path) 返回path的基本名称(即不含父目录及以上路径)。
abspath(path) 返回path的绝对路径。
isabs(path) 如果path是绝对路径名称,则返回True。
normpath(path) 返回标准化路径名称。它将折叠多余分隔符,在Windows上,它把正斜杠转换为反斜杠。
normcase(path) 返回标准化路径名称并转换大小写,在不区分大小写的文件系统上,它把路径全转为小写字母,在Windows上,它把正斜杠转换为反斜杠。
join(path1 [,path2 [, ...]]) 将一个或多个路径组件智能连接为一个路径名称,例如:join('home', 'a', 'b')返回为 '/home/a/b'
split(path) 将path拆分为 (head, tail) 元组,tail为路径基本名称,head是tail之前的内容。相当于 (dirname(), basename())。
splitdrive(path) 将path拆分为 (driver, filename) 元组。在Windows上,drive是驱动器名,在Linux上,drive为空字符串。
splitext(path) 将path拆分为主文件名和后缀(扩展名),如 splittext('a.txt') 的返回值为 ('a', '.txt')
realpath(path) 返回path的真实路径,并除去路径中的所有符号链接(Linux)
relpath(path [,start]) 返回从当前工作目录到path的一条相对路径,可以提供start参数来指定另一个起始目录。
commonpath(list) 返回list中各个元素前缀路径相同的部分,如:os.path.commonpath(['/usr/lib', '/usr/local/lib']),返回:'/usr'
commonprefix(list) 返回list中各个元素前缀相同的部分,如:os.path.commonpath(['/usr/lib', '/usr/local/lib']),返回:'/usr/l'
expanduser(path) 将path中的用户主目录'~'替换成当前用户主目录的绝对路径名称。
expandvars(path) 将path中的'$name'或'${name}'替换成相应环境变量中的值。
其他
supports_unicode_filenames 变量,如果文件系统支持Unicode文件名,那么此变量为True。(一般Windows为True,Linux为False)

 

 

 

 

返回目录

 

 

 

Guess you like

Origin www.cnblogs.com/initcircuit/p/11868525.html