python之路——文件操作

文件操作

文件操作的流程
1.打开文件,并获得一个文件句柄
2.通过句柄进行文件操作
3.关闭文件

文件内容

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
当我年少轻狂
  • 只读模式
f = open('歌词', encoding='utf-8')  # 打开文件 默认使用只读模式打开,编码默认使用系统编码,所以需要指定编码
first_line = f.readline()  # 读一行
print('first line', first_line)
print('我是分割线'.center(50, '*'))
data = f.read()  # 读取剩下的所有内容 对于大文件不应该这样操作
print(data)
f.close()  # 关闭文件

print—>:
first line 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
当我年少轻狂

  • 只写模式
    特性:
    1.只写模式只能进行写操作不能进行其他操作
    2.只写模式会覆盖原文件内容,所以需要谨慎操作
f_write = open('歌词', encoding='utf-8', mode='w')

f_write.write('你好 世界')
f_write.flush()
f_write.close()

f_read = open('歌词', encoding='utf-8', mode='r')
print(f_read.read())
f_read.close()

print—>:
你好 世界

  • 追加模式
    特性:
    1.追加模式会在文件末尾以追加的方式进行写操作
    2.追加模式不会覆盖源文件
    3.追加模式下不能进行都操作
f_append = open('歌词', encoding='utf-8', mode='a')
f_append.write('\n你好 中国')
f_append.close()

f_read = open('歌词', encoding='utf-8', mode='r')
print(f_read.read())
f_read.close()

print—>:
你好 世界
你好 中国

  • “+” 表示可以同时读写某个文件
  • r+模式
f_r_w = open('歌词', encoding='utf-8', mode='r+')
f_r_w.writelines(
    ['first line Somehow, it seems the love I knew was always the most destructive kind\n',
     '不知为何,我经历的爱情总是最具毁灭性的的那种\n']) # 这种写模式会覆盖掉源文件内容
f_r_w.flush()  # 刷新到文件中
f_r_w.seek(0)  # 使文件指针指向初始位置
print(f_r_w.read())
f_r_w.close()

print—>:
first line Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种

  • a+模式
f_r_a = open('歌词', encoding='utf-8', mode='a+')
f_r_a.writelines(['Yesterday when I was young\n',
                  '昨日当我年少轻狂\n',
                  'The taste of life was sweet\n',
                  '生命的滋味是甜的\n'])
f_r_a.flush()
f_r_a.seek(0)
print(f_r_a.read())
f_r_a.close()

print—>:
first line Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂
The taste of life was sweet
生命的滋味是甜的

  • 判断文件读写权限
f = open('歌词', encoding='utf-8', mode='r+')
print(f.readable())
print(f.writable())
f.close()

print—>:
True
True

  • 返回文件底层操作码
f = open('歌词', encoding='utf-8')
print(f.fileno())

print—>:
3

  • 判断文件是否是一个设备文件
f = open('歌词', encoding='utf-8')
print(f.isatty())

print—>:
False

  • 获取当前文件的文件指针位置
f = open('歌词', encoding='utf-8')
f.seek(86)
print(f.tell())

print—>:
86

  • 截断文件
f = open('歌词', encoding='utf-8', mode='r+')
f.writelines(['Yesterday when I was young\n',
                  '昨日当我年少轻狂\n',
                  'The taste of life was sweet\n',
                  '生命的滋味是甜的\n'])
f.truncate(9)
f.seek(0)
print(f.read())
f.close()

print—>:
Yesterday

  • with语句
    为了避免打开文件后忘记关闭文件,可以使用with语句。当with代码块执行完毕时,内部会自动关闭并释放文件资源。
with open('歌词', encoding='utf-8', mode='r+') as f:
    for line in f:
        print(line)

你可能感兴趣的:(python之路)