create account

错乱的记忆:Python 文件读写与曼德拉效应(The Mandela Effect) by oflyhigh

View this thread on: hive.blogpeakd.comecency.com
· @oflyhigh ·
$154.54
错乱的记忆:Python 文件读写与曼德拉效应(The Mandela Effect)
这两天学习block_log修复的问题,大致思路和操作都没问题,现在唯一的问题就是我是用程序计算出偏移量,然后再手工执行`truncate`指令来截短block_log以及block_log.index,很明显我们可以完全让程序来做这事。


![image.png](https://images.hive.blog/DQmeuWMrjYJ81wZjmr7C8XBKgudvJPqiecDr573zHMVaiPo/image.png)
(图源 :[pixabay](https://pixabay.com/illustrations/question-mark-artificial-6786596/))

要让程序做这件事,首先要做的就是打开文件获取文件句柄来读写,在之前的文章中,我提过Python读取二进制文件就四个操作:
* 使用`open`打开文件
* 使用`seek`定位位置
* 使用`read`读取内容
* 使用`close`关闭文件

而读取时,我们要给open函数传入模式(mode)标识`r`(代表读取)以及`b`(代表二进制),也就是说,读取block_log.index文件,需要使用类似如下代码打开:
>`f = open('block_log.index', 'rb')`

那么我们要写文件是不是应该用,以下代码了?
>~~`f = open('block_log.index', 'wb')`~~

话说从业N多年以来,没少和文件打交道,我直觉认为就应该是这样,毕竟`r`和`w`对应,一个读一个写,多完美,多优雅。似乎好多好多年前,我也是这样学的。

然而看了Python参考手册中关于模式的信息,发现我真的是大错特错呢:
>![image.png](https://images.hive.blog/DQmWaxwNY6nKJJiHu7EBeyeSxG7fG5zMq1YEtK54mYaNEvs/image.png)

注意到其中关于`w`的解释了吗:
>open for writing, truncating the file first

作为写模式打开是没错,不过先彻底截短文件!也就是说,如果我用这个`w`来操作block_log,那它就彻底清空了。500G的文件彻底清空,估计我可以哭了。

比如下面这段代码,就可以“帮你”彻底清空block_log,***千万不要试哦***:
>`fd = open('block_log', 'wb')`
>`fd.close()`

所以,根据上边的模式表,要对文件进行***二进制读写***,正确的打开方式应该是:
>`f = open('block_log.index', 'ab+')`

好了,读写的问题解决了,可是为啥我记错了呢?后来我想了想,可能是我把Python和C语言记混淆了,毕竟以前用C比较多。可是查了一下,C和C++的读写模式基本上都和Python类似。


![image.png](https://images.hive.blog/DQmZrWTaCE6uuHLP21SuvPUbJBvmCtzLeMazRnkyZHPDw7P/image.png)
(图源 :[pixabay](https://pixabay.com/illustrations/binary-code-privacy-policy-brain-5137349/))


好吧,应该是我的记忆出偏差了,这让我想起了曼德拉效应:
>曼德拉效应(英文名:The Mandela Effect)是一个心理学效应,指大众对历史的集体记忆与史实不符。主流科学界没有科学研究证明这一“效应”的真实性。

关于曼德拉效应常见的例子有曼德拉是上世纪八九十年代在监狱中死亡还是2013年才去世?讲真2013年听到曼德拉去世的消息我也很震惊,他不是早就去世了吗?Beyond的《光辉岁月》不就唱的他的事迹吗?

还有《西游记》中有没有羊力大仙下油锅的场景,我记得很清楚那油还在翻滚冒泡呢。

类似的还有许多许多,就不一一列举了,大家可以看文末的百度百科链接来参考。

关于曼德拉效应引发的一个思考就是到底是我们的世界被篡改了还是我们的记忆被篡改了?就好比我在文件读写中遇到的`w`模式这个问题。


![image.png](https://images.hive.blog/DQmbZXWVakch6boM3Fpjyp6Gvg6P2A2UzVUH2xxTbRifAHz/image.png)
(图源 :[pixabay](https://pixabay.com/illustrations/artificial-intelligence-brain-think-3382507/))


后来想想,别的事情有没有篡改我不确定,这个`w`模式,八成、大概、可能主要就是我学艺不精,哈哈哈哈哈。

# 相关链接

* [曼德拉效应](https://baike.baidu.com/item/%E6%9B%BC%E5%BE%B7%E6%8B%89%E6%95%88%E5%BA%94/20374535)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 180 others
properties (23)
authoroflyhigh
permlinkpython-the-mandela-effect
categoryhive-105017
json_metadata{"tags":["cn","life","blog"],"image":["https://images.hive.blog/DQmeuWMrjYJ81wZjmr7C8XBKgudvJPqiecDr573zHMVaiPo/image.png","https://images.hive.blog/DQmWaxwNY6nKJJiHu7EBeyeSxG7fG5zMq1YEtK54mYaNEvs/image.png","https://images.hive.blog/DQmZrWTaCE6uuHLP21SuvPUbJBvmCtzLeMazRnkyZHPDw7P/image.png","https://images.hive.blog/DQmbZXWVakch6boM3Fpjyp6Gvg6P2A2UzVUH2xxTbRifAHz/image.png"],"links":["https://pixabay.com/illustrations/question-mark-artificial-6786596/"],"app":"hiveblog/0.1","format":"markdown"}
created2022-02-07 04:28:57
last_update2022-02-07 04:28:57
depth0
children5
last_payout2022-02-14 04:28:57
cashout_time1969-12-31 23:59:59
total_payout_value77.334 HBD
curator_payout_value77.207 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,133
author_reputation6,424,158,810,843,693
root_title"错乱的记忆:Python 文件读写与曼德拉效应(The Mandela Effect)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id110,218,059
net_rshares116,114,202,016,695
author_curate_reward""
vote details (244)
@adyiron ·
我好崇拜O神哥这样IT神,太厉害啦👍
properties (22)
authoradyiron
permlinkre-oflyhigh-202227t201546383z
categoryhive-105017
json_metadata{"tags":["hive-105017","cn","life","blog"],"app":"ecency/3.0.23-mobile","format":"markdown+html"}
created2022-02-07 12:15:48
last_update2022-02-07 12:15:48
depth1
children0
last_payout2022-02-14 12:15:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length18
author_reputation129,620,656,351,470
root_title"错乱的记忆:Python 文件读写与曼德拉效应(The Mandela Effect)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id110,229,392
net_rshares0
@aellly ·
这简直不是人类能懂的。O哥脑子是怎么长的,这么厉害。
我看得头大。
properties (22)
authoraellly
permlinkre-oflyhigh-202227t222631431z
categoryhive-105017
json_metadata{"tags":["cn","life","blog"],"app":"ecency/3.0.21-vision","format":"markdown+html"}
created2022-02-07 14:26:33
last_update2022-02-07 14:26:33
depth1
children0
last_payout2022-02-14 14:26:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length33
author_reputation878,584,760,626,708
root_title"错乱的记忆:Python 文件读写与曼德拉效应(The Mandela Effect)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id110,232,629
net_rshares0
@celeste413 ·
我一直以为曼德拉还活着😂
properties (22)
authorceleste413
permlinkre-oflyhigh-202227t193953976z
categoryhive-105017
json_metadata{"tags":["hive-105017","cn","life","blog"],"app":"ecency/3.0.19-mobile","format":"markdown+html"}
created2022-02-07 11:39:54
last_update2022-02-07 11:39:54
depth1
children0
last_payout2022-02-14 11:39:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length12
author_reputation482,531,417,629,421
root_title"错乱的记忆:Python 文件读写与曼德拉效应(The Mandela Effect)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id110,228,653
net_rshares0
@lovelingling ·
👍
properties (22)
authorlovelingling
permlinkre-oflyhigh-202227t123810744z
categoryhive-105017
json_metadata{"tags":["hive-105017","cn","life","blog"],"app":"ecency/3.0.19-mobile","format":"markdown+html"}
created2022-02-07 04:38:09
last_update2022-02-07 04:38:09
depth1
children0
last_payout2022-02-14 04:38:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1
author_reputation733,266,995,230,770
root_title"错乱的记忆:Python 文件读写与曼德拉效应(The Mandela Effect)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id110,218,446
net_rshares0
@zhangyan-123 ·
之前是看过一个视频,到底是记忆出错还是真的事实被篡改,那个皮卡丘的尾巴上面是否有黑色条纹?真的有点玄乎!🤣🤣🤣
properties (22)
authorzhangyan-123
permlinkre-oflyhigh-202227t214530z
categoryhive-105017
json_metadata{"tags":["hive-105017","cn","life","blog"],"app":"ecency/3.0.19-mobile","format":"markdown+html"}
created2022-02-07 13:04:06
last_update2022-02-07 13:04:06
depth1
children0
last_payout2022-02-14 13:04:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length55
author_reputation925,221,839,600,237
root_title"错乱的记忆:Python 文件读写与曼德拉效应(The Mandela Effect)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id110,230,620
net_rshares0