Python 文件操作

一、Python文件I/O

打印到屏幕

>>> print("Python 是一个非常棒的语言,不是吗?")
Python 是一个非常棒的语言,不是吗?
>>>
>>> import sys
>>> sys.stdout.write("Python 是一个非常棒的语言,不是吗?")
Python 是一个非常棒的语言,不是吗?21
>>> 

读取键盘输入

>>> str = input('please input something here: ')     # python3.x 相当于Python2.x的raw_input()
please input something here: something
>>> print('the content you put is : ', str)
the content you put is :  something
>>> 

打开和关闭文件

open 函数

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

语法:

file object = open(file_name [, access_mode][, buffering])

各个参数的细节如下:

  • file_name:file_name变量是一个包含了你要访问的文件名称的字符串值。
  • access_mode:access_mode决定了打开文件的模式:只读,写入,追加等。所有可取值见如下的完全列表。这个参数是非强制的,默认文件访问模式为只读(r)。
  • buffering:如果buffering的值被设为0,就不会有寄存。如果buffering的值取1,访问文件时会寄存行。如果将buffering的值设为大于1的整数,表明了这就是的寄存区的缓冲大小。如果取负值,寄存区的缓冲大小则为系统默认。

 1 Somehow, it seems the love I knew was always the most destructive kind
 2 不知为何,我经历的爱情总是最具毁灭性的的那种
 3 Yesterday when I was young
 4 昨日当我年少轻狂
 5 The taste of life was sweet
 6 生命的滋味是甜的
 7 As rain upon my tongue
 8 就如舌尖上的雨露
 9 I teased at life as if it were a foolish game
10 我戏弄生命 视其为愚蠢的游戏
11 The way the evening breeze
12 就如夜晚的微风
13 May tease the candle flame
14 逗弄蜡烛的火苗
15 The thousand dreams I dreamed
16 我曾千万次梦见
17 The splendid things I planned
18 那些我计划的绚丽蓝图
19 I always built to last on weak and shifting sand
20 但我总是将之建筑在易逝的流沙上
21 I lived by night and shunned the naked light of day
22 我夜夜笙歌 逃避白昼赤裸的阳光
23 And only now I see how the time ran away
24 事到如今我才看清岁月是如何匆匆流逝
25 Yesterday when I was young
26 昨日当我年少轻狂
27 So many lovely songs were waiting to be sung
28 有那么多甜美的曲儿等我歌唱
29 So many wild pleasures lay in store for me
30 有那么多肆意的快乐等我享受
31 And so much pain my eyes refused to see
32 还有那么多痛苦 我的双眼却视而不见
33 I ran so fast that time and youth at last ran out
34 我飞快地奔走 最终时光与青春消逝殆尽
35 I never stopped to think what life was all about
36 我从未停下脚步去思考生命的意义
37 And every conversation that I can now recall
38 如今回想起的所有对话
39 Concerned itself with me and nothing else at all
40 除了和我相关的 什么都记不得了
41 The game of love I played with arrogance and pride
42 我用自负和傲慢玩着爱情的游戏
43 And every flame I lit too quickly, quickly died
44 所有我点燃的火焰都熄灭得太快
45 The friends I made all somehow seemed to slip away
46 所有我交的朋友似乎都不知不觉地离开了
47 And only now I'm left alone to end the play, yeah
48 只剩我一个人在台上来结束这场闹剧
49 Oh, yesterday when I was young
50 噢 昨日当我年少轻狂
51 So many, many songs were waiting to be sung
52 有那么那么多甜美的曲儿等我歌唱
53 So many wild pleasures lay in store for me
54 有那么多肆意的快乐等我享受
55 And so much pain my eyes refused to see
56 还有那么多痛苦 我的双眼却视而不见
57 There are so many songs in me that won't be sung
58 我有太多歌曲永远不会被唱起
59 I feel the bitter taste of tears upon my tongue
60 我尝到了舌尖泪水的苦涩滋味
61 The time has come for me to pay for yesterday
62 终于到了付出代价的时间 为了昨日
63 When I was young
64 当我年少轻狂
yesterday.lrc
#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# Author: antcolonies

# data = open('yesterday1.lrc')
# 默认为系统的编码格式
# UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 106: illegal multibyte sequence

# data = open('yesterday1.lrc',encoding='utf-8')
# data: <_io.TextIOWrapper name='yesterday1.lrc' mode='r' encoding='utf-8'>

f = open('yesterday1.lrc',encoding='utf-8')  # 内存对象f,在文件操作中称为句柄
print(f)
# f: <_io.TextIOWrapper name='yesterday1.lrc' mode='r' encoding='utf-8'>
# 默认打开的模式为'读'
data = f.read()           # 读取整个文件
data2 = f.read()
print(id(data),id(data2))  #  42019856 6715464
f.write('Love u')  # io.UnsupportedOperation: not writable

f = open('yesterday2.lrc','w',encoding='utf-8')
# print(f)   <_io.TextIOWrapper name='yesterday2.lrc' mode='w' encoding='utf-8'>
# 'w'模式下打开会创建一个yesterday2.lrc名称的文件,若文件存在,则覆盖原文件
data = f.read()      # io.UnsupportedOperation: not readable
f.write('Love u
')
f.write('Hate u')

f = open('yesterday2.lrc','a',encoding='utf-8')
print(f)
# f: <_io.TextIOWrapper name='yesterday2.lrc' mode='a' encoding='utf-8'>
# a(append) 末尾追加, 若文件不存在,则先创建,再追加
f.write('Love u
')
f.write('Hate u
')
f.write('when i was yong i listen to the radio
')
f.read()       # io.UnsupportedOperation: not readable


# 读取前5行
f = open('yesterday1.lrc','r',encoding='utf-8')
for I in range(5):
    print(f.readline())    # f.readline()   读取一行

# 读取整个文件,第十行不打印
f = open('yesterday1.lrc','r',encoding='utf-8')
print(f.readlines()) #读取整个文件,并以行为元素,按照文件内容顺序组成一个列表,比较适合读取小文件
['Somehow, it seems the love I knew was always the most destructive kind
', '不知为何,我经历的爱情总是最具毁灭性的的那种
', 'Yesterday when I was young
', '昨日当我年少轻狂
', 'The taste of life was sweet
', '生命的滋味是甜的
', 'As rain upon my tongue
', '就如舌尖上的雨露
', 'I teased at life as if it were a foolish game
', '我戏弄生命 视其为愚蠢的游戏
', 'The way the evening breeze
', '就如夜晚的微风
', 'May tease the candle flame
', '逗弄蜡烛的火苗
', 'The thousand dreams I dreamed
', '我曾千万次梦见
', 'The splendid things I planned
', '那些我计划的绚丽蓝图
', 'I always built to last on weak and shifting sand
', '但我总是将之建筑在易逝的流沙上
', 'I lived by night and shunned the naked light of day
', '我夜夜笙歌 逃避白昼赤裸的阳光
', 'And only now I see how the time ran away
', '事到如今我才看清岁月是如何匆匆流逝
', 'Yesterday when I was young
', '昨日当我年少轻狂
', 'So many lovely songs were waiting to be sung
', '有那么多甜美的曲儿等我歌唱
', 'So many wild pleasures lay in store for me
', '有那么多肆意的快乐等我享受
', 'And so much pain my eyes refused to see
', '还有那么多痛苦 我的双眼却视而不见
', 'I ran so fast that time and youth at last ran out
', '我飞快地奔走 最终时光与青春消逝殆尽
', 'I never stopped to think what life was all about
', '我从未停下脚步去思考生命的意义
', 'And every conversation that I can now recall
', '如今回想起的所有对话
', 'Concerned itself with me and nothing else at all
', '除了和我相关的 什么都记不得了
', 'The game of love I played with arrogance and pride
', '我用自负和傲慢玩着爱情的游戏
', 'And every flame I lit too quickly, quickly died
', '所有我点燃的火焰都熄灭得太快
', 'The friends I made all somehow seemed to slip away
', '所有我交的朋友似乎都不知不觉地离开了
', "And only now I'm left alone to end the play, yeah
", '只剩我一个人在台上来结束这场闹剧
', 'Oh, yesterday when I was young
', '噢 昨日当我年少轻狂
', 'So many, many songs were waiting to be sung
', '有那么那么多甜美的曲儿等我歌唱
', 'So many wild pleasures lay in store for me
', '有那么多肆意的快乐等我享受
', 'And so much pain my eyes refused to see
', '还有那么多痛苦 我的双眼却视而不见
', "There are so many songs in me that won't be sung
", '我有太多歌曲永远不会被唱起
', 'I feel the bitter taste of tears upon my tongue
', '我尝到了舌尖泪水的苦涩滋味
', 'The time has come for me to pay for yesterday
', '终于到了付出代价的时间 为了昨日
', 'When I was young
', '当我年少轻狂']
for index,line in enumerate(f.readlines()):   # 枚举
    if index == 9:
        print('-------------------优美的分割线-----------')
        continue
    print(line.strip())

# 对于处理大文件(比如大小为4G),f.readline()或f.readlines(),会将文件读出并存储于内存中,严重的消耗内存
f = open('yesterday1.lrc','r',encoding='utf-8')
count = 0
for line in f:      # 内存中始终存储一行,效率最高,此时f对象被视为迭代器
    if count == 9:
        print('-------------------优美的分割线-----------')
        count += 1
        continue
    print(line.strip())
    count += 1

# f = open('yesterday2.lrc','r+',encoding='utf-8')  # 读写
# f = open('yesterday2.lrc','w+',encoding='utf-8')  # 写读
# f = open('yesterday2.lrc','a+',encoding='utf-8')    # 追加读

# print(f.readline())
# print(f.readline())
# print(f.readline())
# print(f.tell())
# f.write('---------------perfect---------------
')
# f.write('---------------perfect---------------
')
# f.write('---------------perfect---------------
')
# f.write('---------------perfect---------------
')
# print(f.tell())
# f.seek(10)
# print(f.readline())
# f.write('should be at the begining of the second line.
')

# f = open('yesterday2.lrc','rb',encoding='utf-8')    # 二进制读
# ValueError: binary mode doesn't take an encoding argument
# f = open('yesterday2.lrc','rb')    # 二进制读  (网络传输)
# print(f.readline())
# print(f.readline())
# print(f.readline())
'''
b'---------------perfect---------------
'
b'---------------perfect---------------
'
b'---------------perfect---------------
'
'''

f = open('yesterday2.lrc','wb')
# f.write('hello, binary')
# TypeError: 'str' does not support the buffer interface
f.write('hello, binary
'.encode())   # 默认为'utf-8'

f.close()
# 手动关闭文件
open_rwab+

close()方法

File 对象的 close()方法刷新缓冲区里任何还没写入的信息,并关闭该文件,这之后便不能再进行写入。

当一个文件对象的引用被重新指定给另一个文件时,Python 会关闭之前的文件。用 close()方法关闭文件是一个很好的习惯。

File对象的属性

一个文件被打开后,你有一个file对象,你可以得到有关该文件的各种信息。
以下是和file对象相关的所有属性的列表:
属性             描述
file.closed     返回true如果文件已被关闭,否则返回false。
file.mode      返回被打开文件的访问模式。
file.name      返回文件的名称。

f = open('yesterday2.lrc','wb')

print(f.closed)
print(f.mode)
print(f.name)

'''
False
wb
yesterday2.lrc
'''

读写文件

write()方法

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

#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# Author: antcolonies

f = open('foo.txt','wb')
# f.write('www.runoob.com!
Very good site!
')
# TypeError: 'str' does not support the buffer interface

f.write('www.runoob.com!
Very good site!
'.encode())

f.close()
read()方法

read()方法从一个打开的文件中读取一个字符串。需要重点注意的是,Python字符串可以是二进制数据,而不是仅仅是文字。

语法:

fileObject.read([count]);

在这里,被传递的参数是要从已打开文件中读取的字节计数。该方法从文件的开头开始读入,如果没有传入count,它会尝试尽可能多地读取更多的内容,很可能是直到文件的末尾。

#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# Author: antcolonies

f = open('foo.txt','r')
print(f.read(10))      # www.runoob

f.close()

文件定位

tell()方法告诉你文件内的当前位置;换句话说,下一次的读写会发生在文件开头这么多字节之后。

seek(offset)方法改变当前文件的位置。Offset变量表示相对于文件开始(0)要移动的字节数。

例子:

就用我们上面创建的文件foo.txt。

#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# Author: antcolonies

f = open('foo.txt','r',encoding='utf-8')

print(f.tell())
print(f.read(10))      # www.runoob
print(f.tell())

f.close()

重命名和删除文件

Python的os模块提供了帮你执行文件处理操作的方法,比如重命名和删除文件。

要使用这个模块,你必须先导入它,然后才可以调用相关的各种功能。

rename()方法

rename()方法需要两个参数,当前的文件名和新文件名。

语法:

os.rename(current_file_name, new_file_name)

例子:

#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# Author: antcolonies

import os

# 重命名文件foo.txt到foo.lrc
os.rename('foo.txt','foo.lrc')

remove()方法

你可以用remove()方法删除文件,需要提供要删除的文件名作为参数。

#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# Author: antcolonies

import os

# 删除一个已经存在的文件
os.remove('foo.lrc')

Python目录的创建、修改和删除,目录的切换和当前目录的获取

os.mkdir()

os.rename()

os.getcwd()

os.chdir()

os.rmdir()

#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# Author: antcolonies

import os

os.mkdir(r'e:/python')  # make directory in current directory
os.rename(r'e:/python',r'e:/Python')
print(os.getcwd())      # E:python14_workspaces14day03
os.chdir(r'F:Music')   # change directory
print(os.getcwd())      # F:Music      (getcwd--get current working directory)
os.rmdir(r'e:/Python')  # remove directory

二、Python file 方法

reference to : http://www.runoob.com/python/file-methods.html

 1 #!/usr/bin/evn python
 2 # -*- coding:utf-8 -*-
 3 # Author: antcolonies
 4 
 5 f = open('yesterday1.lrc','r',encoding='utf-8')
 6 print(f.tell())    # f.tell() 查看目前光标所在位置
 7 print(f.read())    # 默认读取整个文件(不包括换行符)
 8 print(f.read(10))  # 从第一个字符开始,连续读取5个字符
 9 print(f.tell())
10 f.seek(0)
11 print(f.readline())
12 print(f.tell())
13 f.seek(10)         # 跳转至第10个字符处
14 print(f.tell())
15 print(f.readline())   # t seems the love I knew was always the most destructive kind
16 
17 f.detach()
18 print(f.encoding)
19 print(f.fileno())     # 3,返回该文件在内存的编号(不是内存地址)
20 print(f.name)         # yesterday1.lrc
21 print(f.seekable())   # 文件是否可跳转
22 print(f.readable())
23 print(f.writable())
24 
25 print(dir(f.buffer))
26 f.flush()        # 将写入给文件(尚在缓存)的内容强制刷新写入硬盘
27 import sys,time
28 for i in range(20):
29     sys.stdout.write('#')       # 标准输出(往控制台打印且不换行)
30     sys.stdout.flush()
31     time.sleep(0.1)
32 
33 print(f.closed)        # 文件是否关闭
34 
35 f = open('yesterday2.lrc','a',encoding='utf-8')
36 # io.UnsupportedOperation: File not open for writing
37 f.truncate()        # 默认清空文件
38 f.seek(20)
39 print(f.tell())
40 f.truncate(40)        # 从文件0处截取40个字符
41 print(f.tell())
file_object_method

对文件操作流程

  1. 打开文件,得到文件句柄并赋值给一个变量
  2. 通过句柄对文件进行操作
  3. 关闭文件

打开文件的模式有:

  • r,只读模式(默认)。
  • w,只写模式。【不可读;不存在则创建;存在则删除内容;】
  • a,追加模式。【可读;   不存在则创建;存在则只追加内容;】

"+" 表示可以同时读写某个文件

  • r+,可读写文件。【可读;可写;可追加】
  • w+,写读
  • a+,同a

"U"表示在读取时,可以将 自动转换成 (与 r 或 r+ 模式同使用)

  • rU
  • r+U

"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)

  • rb
  • wb
  • ab

with语句

为了避免打开文件后忘记关闭,可以通过管理上下文,即:

with open('log','r') as f:
     
    ...

如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。

在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:

with open('log1') as obj1, open('log2') as obj2:
    pass
 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author: antcolonies
 4 
 5 import sys
 6 
 7 f = open('yesterday2.lrc','r',encoding='utf-8')
 8 f_new = open('yesterday2.bak','w',encoding='utf-8')
 9 
10 search_str = sys.argv[1]
11 replace_str = sys.argv[2]
12 
13 for line in f:
14     if search_str in line:
15         line = line.replace(search_str,replace_str)
16     f_new.write(line)
17 
18 f.close()
19 f_new.close()
file_modify.py
 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author: antcolonies
 4 
 5 import sys
 6 
 7 search_str = sys.argv[1]
 8 replace_str = sys.argv[2]
 9 
10 with open('yesterday2.lrc','r',encoding='utf-8') as f,
11     open('yesterday2.lrc.bak','w',encoding='utf-8') as f_new:
12     for line in f:
13         if search_str in line:
14             line = line.replace(search_str,replace_str)
15         f_new.write(line)
with.py

程序练习  

程序1: 实现简单的shell sed替换功能

 1 #!/usr/local/Python-3.5.2/python
 2 # -*- coding:utf-8 -*-
 3 # antcolonies
 4 
 5 import sys,os
 6 
 7 search_str = sys.argv[1]
 8 ori_str = sys.argv[2]
 9 replace_str = sys.argv[3]
10 filename = sys.argv[4]
11 filename_1 = filename + '_1'
12 
13 with open(filename,'r',encoding='utf-8') as f ,
14    open(filename_1,'w',encoding='utf-8') as f_new:
15     for line in f:
16         if search_str in line:
17             line = line.replace(ori_str,replace_str)
18         f_new.write(line)
19 
20 os.rename(filename_1,filename)
sed.py

程序2:修改haproxy配置文件

 1 1、查
 2     输入:www.oldboy.org
 3     获取当前backend下的所有记录
 4 
 5 2、新建
 6     输入:
 7         arg = {
 8             'backend': 'www.oldboy.org',
 9             'record':{
10                 'server': '100.1.7.9',
11                 'weight': 20,
12                 'maxconn': 30
13             }
14         }
15 
16 3、删除
17     输入:
18         arg = {
19             'backend': 'www.oldboy.org',
20             'record':{
21                 'server': '100.1.7.9',
22                 'weight': 20,
23                 'maxconn': 30
24             }
25         }
method
 1 global       
 2         log 127.0.0.1 local2
 3         daemon
 4         maxconn 256
 5         log 127.0.0.1 local2 info
 6 defaults
 7         log global
 8         mode http
 9         timeout connect 5000ms
10         timeout client 50000ms
11         timeout server 50000ms
12         option  dontlognull
13 
14 listen stats :8888
15         stats enable
16         stats uri       /admin
17         stats auth      admin:1234
18 
19 frontend oldboy.org
20         bind 0.0.0.0:80
21         option httplog
22         option httpclose
23         option  forwardfor
24         log global
25         acl www hdr_reg(host) -i www.oldboy.org
26         use_backend www.oldboy.org if www
27 
28 backend www.oldboy.org
29         server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
haproxy.config
 1 #!/usr/local/Python-3.5.2/python
 2 # -*- coding:utf-8 -*-
 3 # antcolonies
 4 
 5 import sys,os
 6 
 7 filename = sys.argv[1]
 8 
 9 def search(string):
10     flag = ''
11     with open(filename,'r',encoding='utf-8') as f:
12         for line in f:
13             if string in line:
14                 if len(line)==len(line.lstrip()):
15                     flag = True
16             if flag:
17                 print(line.rstrip())
18                 if line.strip() == '':
19                     break
20 
21 def create(arg):
22     with open(filename,'r',encoding='utf-8') as f:
23         listfile = f.readlines()
24         lastline = listfile[-1]
25         arg_str = '%s %s %s %d %s %d' %('server',arg['record']['server'],'weight',
26     arg['record']['weight'],'maxconn',arg['record']['maxconn'])
27         if lastline.strip() == arg_str:
28             print('the record you put has already existed!')
29             return False
30         indent = len(lastline) - len(lastline.lstrip())
31         indentstr = ''
32         for i in range(indent):
33             indentstr += ' ' 
34         arg_str = '%s%s' %(indentstr,arg_str)
35     with open(filename,'a',encoding='utf-8') as f:
36         f.write('%s%s %s %s %d %s %d' %(indentstr,'server',arg['record']['server'],
37     'weight',arg['record']['weight'],'maxconn',arg['record']['maxconn']))
38 
39 
40 def delete(arg):
41     filename_1 = filename + '_1'
42     record = '%s %s %s %d %s %d' %('server',arg['record']['server'],'weight',
43     arg['record']['weight'],'maxconn',arg['record'][    'maxconn'])
44     with open(filename,'r',encoding='utf-8') as f,
45     open(filename_1,'w',encoding='utf-8') as f_new:
46         for line in f:
47             if record == line.strip():
48                 continue
49             f_new.write(line)
50     os.rename(filename_1,filename)
51         
52 
53 while True:
54     action = input('(search|create|delete)')
55     if action == 'search':
56         record = input('select a title: ')
57         search(record)
58     elif action == 'create':
59         arg = input('input an argv: ')
60         arg_list = eval(arg)
61         create(arg_list)
62     elif action == 'delete':
63         arg = input('input an argv: ')
64         arg_list = eval(arg)
65         delete(arg_list)
66     else:
67         print('you put a wrong action!')
68     stat = input("q(quit) or any other keywords to continue: ")
69     if stat == 'q':
70         break
code
原文地址:https://www.cnblogs.com/ant-colonies/p/6637121.html