帮朋友写一段短小的Python代码,其每次运行时会产生一个字符串,需要与上次运行时生成的结果字符串对比一下,来判断是否需要继续。 ![](https://cdn.pixabay.com/photo/2018/09/19/18/30/keyboard-3689225_960_720.jpg) (图源 :[pixabay](https://pixabay.com/)) 要想做到这点有很多办法,比如说读写数据库啊、读写配置文件啊,但是无论那样都觉得太麻烦了,毕竟我们没有太多的数据要存储,不过是一个字符串。 想来想去,最简单的办法莫过于直接写个文本文件了,因为既要读取又要写入,于是我直接写了如下打开文件的代码: >`f = open(fname, 'rw')` 其中`fname`保存着路径+文件名,看着觉得挺完美的,然而执行时直接报错了: >ValueError: must have exactly one of create/read/write/append mode 擦,如果只是这些模式,我怎么一边愉快地读,一边愉快地写了?网上找了一些内容,有建议读模式打开文件同时写模式再打开,还有的建议打开A文件读,再打开B文件写。 如果打开A读,再打开B写,那么我需要关闭A后,在用B覆盖A,那样好像还不如我打开并读A,关闭A,再打开写A。尽管这个思路应该完全可行,但是Python真的不能用读写方式打开吗? 于是找了一下Python官网上的[关于open的内容](https://docs.python.org/3/library/functions.html#open),并看到这部分: >![image.png](https://images.hive.blog/DQmYqSdcSwUUdLtGReZBpKnUhNsgeaiTY3anuMy3YFaNMVj/image.png) 尤其是其中`'+' | open for updating (reading and writing)`简直就是我想要的啊,虽然我按我的经验`+`是要和其它标记一起用的。但是还是不死心试了一下: >`f = open(fname, '+')` 果不其然出错了: >ValueError: Must have exactly one of create/read/write/append mode and at most one plus 还是老老实实用`r+`吧,亦即`f = open(fname, 'r+')`果然打开没有问题。但是新问题又来了,我读完字符串并写入后,新新写入的内容直接追加到原来的内容后边了。 不过想想也是,因为读完文件内容的时候,文件指针已经指到文件末尾了,在这个位置写入,当然就是追加喽。 或许我们可以用如下指令将指针移动到文件最前端: >`f.seek(0)` 不过想想,这样或许也不安全,假设当前文件内的字符串为`1234567890`,我要新写入的字符串为`12345`,那么你写完以后,字符串的内容变不变呢? 我没去测试字符串的内容变不变,但是我觉得从逻辑上就不优雅。为了优雅的处理,我还是用如下代码吧: >`f.seek(0)` >`f.truncate()` >`f.write(str_a)` 也就是说先定位到文件头,再清空内容,再写入,这样看起来优雅多了。把程序跑起来,果然好用,这样我就放心啦。 # 相关链接 * https://docs.python.org/3/library/functions.html#open * https://docs.python.org/3.7/tutorial/inputoutput.html#methods-of-file-objects
post_id | 85,998,010 |
---|---|
author | oflyhigh |
permlink | python-and-r |
category | cn |
json_metadata | {"tags":["cn","cutehive","cn-programming","python","study"],"image":["https:\/\/cdn.pixabay.com\/photo\/2018\/09\/19\/18\/30\/keyboard-3689225_960_720.jpg","https:\/\/images.hive.blog\/DQmYqSdcSwUUdLtGReZBpKnUhNsgeaiTY3anuMy3YFaNMVj\/image.png"],"links":["https:\/\/pixabay.com\/","https:\/\/docs.python.org\/3\/library\/functions.html#open","https:\/\/docs.python.org\/3.7\/tutorial\/inputoutput.html#methods-of-file-objects"],"app":"steemit\/0.2","format":"markdown"} |
created | 2020-05-14 03:14:03 |
last_update | 2020-05-14 03:14:03 |
depth | 0 |
children | 0 |
net_rshares | 83,235,992,286,034 |
last_payout | 2020-05-21 03:14:03 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 21.918 SBD |
curator_payout_value | 18.585 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 1,576 |
author_reputation | 1,148,153,621,496,884 |
root_title | "每天进步一点点:Python读写文件的模式 & r+" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
adm | 0 | 29,519,381,254,736 | 100% | ||
thecryptodrive | 0 | 600,022,613,177 | 25% | ||
gerber | 0 | 166,542,449,810 | 14.8% | ||
kosimoos | 0 | 564,863,881 | 50% | ||
daan | 0 | 68,589,005,877 | 8% | ||
gidlark | 0 | 5,535,808,204 | 10% | ||
blockchainbilly | 0 | 8,288,072,488 | 50% | ||
fundurian | 0 | 35,919,790,243 | 98% | ||
deanliu | 0 | 526,605,024,616 | 100% | ||
exyle | 0 | 469,231,120,446 | 10% | ||
sharker | 0 | 2,459,862,029 | 11% | ||
rea | 0 | 1,528,360,393,384 | 40% | ||
yogi | 0 | 10,880,630,858 | 25% | ||
joythewanderer | 0 | 52,200,308,145 | 50% | ||
lemooljiang | 0 | 637,997,199,094 | 88% | ||
ace108 | 0 | 272,856,041,386 | 25% | ||
kibela | 0 | 3,044,530,544 | 11% | ||
laoyao | 0 | 42,199,924,600 | 100% | ||
somebody | 0 | 10,286,454,063 | 100% | ||
skysunny | 0 | 135,304,154 | 88% | ||
midnightoil | 0 | 150,878,902,774 | 100% | ||
xiaohui | 0 | 835,869,015,100 | 100% | ||
silentlucidity53 | 0 | 0 | 75% | ||
oflyhigh | 0 | 1,337,780,829,368 | 100% | ||
yulan | 0 | 15,165,071,203 | 100% | ||
rivalhw | 0 | 422,650,749,237 | 100% | ||
nextgen622 | 0 | 686,126,220,769 | 100% | ||
helene | 0 | 463,343,710,680 | 100% | ||
ffcrossculture | 0 | 476,734,620,030 | 100% | ||
sweetsssj | 0 | 18,564,901,195,248 | 50% | ||
ebargains | 0 | 7,857,146,402 | 30% | ||
funnyman | 0 | 231,572,550,735 | 25% | ||
lexikon082 | 0 | 1,257,316,811 | 100% | ||
dapeng | 0 | 35,986,284,513 | 100% | ||
timool | 0 | 1,190,720,608 | 88% | ||
lucknie | 0 | 18,612,402,837 | 88% | ||
dumping | 0 | 1,901,708,022 | 88% | ||
justinashby | 0 | 18,569,575,101 | 100% | ||
lordneroo | 0 | 4,672,094,230 | 14.06% | ||
austinsandersco | 0 | 697,923,276 | 70% | ||
teachblogger | 0 | 2,104,076,333 | 50% | ||
kingofdew | 0 | 45,602,843,148 | 100% | ||
htliao | 0 | 24,366,856,324 | 20% | ||
curly-q | 0 | 2,274,788,621 | 100% | ||
eliel | 0 | 57,570,887,317 | 30% | ||
frankintaiwan | 0 | 0 | 10% | ||
wylo | 0 | 620,254,764 | 100% | ||
jkkim | 0 | 26,748,437 | 10% | ||
ebejammin | 0 | 5,841,285,953 | 100% | ||
improv | 0 | 19,256,942,676 | 4.5% | ||
nuagnorab | 0 | 79,352,507,203 | 20% | ||
cryptohustler | 0 | 38,708,929,865 | 100% | ||
eval | 0 | 761,591,802 | 100% | ||
anacristinasilva | 0 | 27,794,441,214 | 30% | ||
speeding | 0 | 3,689,072,665 | 100% | ||
maxer27 | 0 | 26,717,147,875 | 20% | ||
blacklux | 0 | 14,876,694,713 | 25% | ||
walkinharmony | 0 | 39,003,522,252 | 40% | ||
techken | 0 | 795,716,669 | 3% | ||
asterix87 | 0 | 14,081,518,495 | 100% | ||
gniksivart | 0 | 36,283,765,321 | 30% | ||
susanli3769 | 0 | 142,323,391,233 | 100% | ||
abetterworld | 0 | 2,931,994,796 | 100% | ||
runicar | 0 | 51,174,562,047 | 14.06% | ||
that1consultant | 0 | 252,556,455 | 100% | ||
sanzo | 0 | 478,176,305 | 100% | ||
davaowhenyo | 0 | 546,782,998 | 100% | ||
allenshayzar | 0 | 547,299,912 | 100% | ||
dado13btc | 0 | 1,704,096,340 | 14.06% | ||
travelgirl | 0 | 62,382,242,362 | 40% | ||
raku | 0 | 244,779,148 | 100% | ||
resteeming | 0 | 67,466,666 | 100% | ||
ravenousappetite | 0 | 606,895,480 | 100% | ||
aabb | 0 | 326,653,208 | 100% | ||
catwomanteresa | 0 | 60,050,775,155 | 50% | ||
auntigormint | 0 | 67,526,715 | 100% | ||
mrliga | 0 | 21,434,587,472 | 100% | ||
oldman28 | 0 | 22,753,911,562 | 48% | ||
sweethoney | 0 | 2,025,775,469 | 100% | ||
stinawog | 0 | 5,124,523,523 | 30% | ||
liangfengyouren | 0 | 3,390,066,333 | 50% | ||
lancy | 0 | 4,742,716,507 | 92% | ||
sailor01 | 0 | 4,411,768,214 | 100% | ||
aafeng | 0 | 92,848,979,630 | 100% | ||
cn-reader | 0 | 5,514,474,869 | 50% | ||
pardeepkumar | 0 | 9,034,009,901 | 14.06% | ||
tvb | 0 | 20,211,639,400 | 30% | ||
skenan | 0 | 6,394,370,248 | 50% | ||
bearpaw | 0 | 422,872,650 | 100% | ||
rocky1 | 0 | 4,026,555,269,925 | 4% | ||
kimzwarch | 0 | 7,570,544,216 | 4% | ||
bboyady | 0 | 878,125,750 | 5% | ||
saltiberra | 0 | 1,080,618,469 | 10% | ||
freedom-fighter | 0 | 546,493,330 | 100% | ||
accelerator | 0 | 95,086,914,812 | 13.2% | ||
chinchilla | 0 | 224,984,713,452 | 100% | ||
estream.studios | 0 | 1,316,872,483 | 30% | ||
technologynepal | 0 | 546,281,942 | 100% | ||
davidke20 | 0 | 11,891,212,750 | 10% | ||
rosatravels | 0 | 30,477,296,722 | 5% | ||
osm0sis | 0 | 9,649,624,743 | 30% | ||
robertoueti | 0 | 1,969,573,743 | 3% | ||
roleerob | 0 | 13,620,158,032 | 4.44% | ||
khalilad | 0 | 540,316,042 | 100% | ||
ms8988 | 0 | 194,416,357 | 100% | ||
dobartim | 0 | 449,352,715,138 | 37% | ||
espoem | 0 | 5,681,598,047 | 5% | ||
xiaoshancun | 0 | 23,034,250,263 | 100% | ||
rakkasan84 | 0 | 282,069,199,090 | 24% | ||
hqy | 0 | 6,642,749,771 | 15% | ||
rollsman | 0 | 5,905,332,278 | 100% | ||
stakuza | 0 | 67,198,669 | 100% | ||
tatjanastan | 0 | 427,036,710,549 | 42% | ||
toocurious | 0 | 14,919,579,473 | 14.65% | ||
metten | 0 | 9,960,541,613 | 88% | ||
ikonik | 0 | 69,468,220 | 100% | ||
vfxness | 0 | 17,564,599 | 100% | ||
lemminon | 0 | 547,050,194 | 100% | ||
heyeshuang | 0 | 607,619,547 | 100% | ||
pataty69 | 0 | 27,131,346,483 | 20% | ||
razor80 | 0 | 538,240,909 | 100% | ||
fastiduos | 0 | 606,881,285 | 100% | ||
winniex | 0 | 8,713,270,067 | 20% | ||
rebecca80 | 0 | 148,716,191 | 100% | ||
weavingwords | 0 | 60,252,389,236 | 100% | ||
nitro.live | 0 | 255,262,196 | 100% | ||
fknmayhem | 0 | 1,630,588,228 | 7.4% | ||
blockbrothers | 0 | 13,300,793,236 | 5% | ||
cn-book | 0 | 1,035,274,646 | 88% | ||
red-rose | 0 | 4,301,907,012 | 30% | ||
huatanzhang | 0 | 852,300,703 | 50% | ||
emrebeyler | 0 | 68,214,628,783 | 14.8% | ||
chann | 0 | 6,600,481,640 | 20% | ||
cn-movie | 0 | 117,633,543 | 88% | ||
etherpunk | 0 | 1,420,112,153 | 25% | ||
ocupation | 0 | 13,782,139,518 | 14.06% | ||
itchyfeetdonica | 0 | 116,101,660,318 | 50% | ||
vivia | 0 | 3,196,782,636 | 88% | ||
coindzs | 0 | 1,716,118,621 | 100% | ||
enjoyinglife | 0 | 5,328,040,607 | 14.06% | ||
fourfourfun | 0 | 1,623,018,083 | 6.25% | ||
mmmmkkkk311 | 0 | 369,749,332,625 | 100% | ||
bengy | 0 | 890,627,943 | 0.66% | ||
yikloongye | 0 | 0 | 10% | ||
johnhooi | 0 | 9,680,434 | 10% | ||
nealmcspadden | 0 | 189,205,384,205 | 14.8% | ||
maiyude | 0 | 138,663,579,543 | 50.79% | ||
suhunter | 0 | 952,991,866 | 50% | ||
curx | 0 | 102,169,581,853 | 30% | ||
xiaoli | 0 | 302,282,260 | 88% | ||
yumisee | 0 | 83,781,772 | 10% | ||
casberp | 0 | 4,740,709,975 | 3% | ||
joeliew | 0 | 303,538,054 | 10% | ||
investprosper | 0 | 4,367,071,191 | 7.4% | ||
hkit98 | 0 | 0 | 10% | ||
elvinmas001 | 0 | 0 | 10% | ||
watersoo | 0 | 282,018,618 | 10% | ||
vamos-amigo | 0 | 246,834,653 | 10% | ||
cadawg | 0 | 14,034,311,931 | 10.36% | ||
sudefteri | 0 | 14,730,287,959 | 100% | ||
happy-soul | 0 | 32,600,116,199 | 10% | ||
florenceboens | 0 | 794,104,418 | 10% | ||
iipoh06 | 0 | 7,667,934 | 10% | ||
ofildutemps | 0 | 1,368,826,245 | 30% | ||
historylover | 0 | 150,087,261 | 100% | ||
annabellenoelle | 0 | 3,431,227 | 10% | ||
mindtrap | 0 | 69,595,071,454 | 14.06% | ||
yethui | 0 | 24,516,900 | 10% | ||
btccurrency1 | 0 | 419,120,307 | 100% | ||
riczlook | 0 | 11,583,742 | 10% | ||
ethanlee | 0 | 4,118,633,056 | 100% | ||
andrewnoel | 0 | 0 | 10% | ||
retinox | 0 | 2,902,177,650 | 2.49% | ||
fishbb | 0 | 1,449,310,789 | 25.39% | ||
outlinez | 0 | 737,879,640 | 4% | ||
fredo77200 | 0 | 3,665,154,696 | 100% | ||
bestboom | 0 | 6,311,001,696 | 14.8% | ||
onepercentbetter | 0 | 1,386,143,851 | 0.5% | ||
abrockman | 0 | 25,990,138,553 | 7.4% | ||
fanso | 0 | 1,690,869,437 | 100% | ||
mariuszkarowski | 0 | 9,490,921,309 | 100% | ||
cryptoknight27 | 0 | 0 | 100% | ||
khayziljoy | 0 | 328,099,193 | 100% | ||
therising | 0 | 4,572,534,037,214 | 20% | ||
sustainablelivin | 0 | 1,884,916,238 | 15% | ||
fishdd | 0 | 1,016,835,900 | 25.39% | ||
angelinafx | 0 | 1,495,102,746 | 4.5% | ||
garrettwallace | 0 | 1,364,213,436 | 18% | ||
payroll | 0 | 1,322,630,134,664 | 10% | ||
adeyemidrey | 0 | 57,249,721,440 | 100% | ||
jaydih | 0 | 237,182,261,539 | 5% | ||
andrewma | 0 | 5,072,261,104 | 20% | ||
promobot | 0 | 50,685,032,038 | 25% | ||
kimseun | 0 | 33,085,194,255 | 5% | ||
mynotsofitlife | 0 | 934,822,639 | 10% | ||
steem.services | 0 | 1,469,939,706,873 | 30% | ||
crypto.piotr | 0 | 42,950,224,307 | 5% | ||
pladozero | 0 | 19,918,079,628 | 10% | ||
nateaguila | 0 | 76,867,946,449 | 8% | ||
hmayak | 0 | 23,695,953,384 | 100% | ||
sergino | 0 | 11,217,434,769 | 35% | ||
archisteem | 0 | 4,695,272,128 | 7.5% | ||
cn-malaysia | 0 | 2,800,485,417 | 10% | ||
merlion | 0 | 145,249,243 | 0.5% | ||
digital.mine | 0 | 27,865,520,882 | 1% | ||
kahvesizlik | 0 | 1,186,377,248 | 100% | ||
moneybaby | 0 | 846,048,497 | 2.5% | ||
kanadaramagi123 | 0 | 22,485,465,179 | 5% | ||
suonghuynh | 0 | 73,529,133,876 | 4.5% | ||
abcallen | 0 | 17,031,942,847 | 80% | ||
loveecho | 0 | 41,627,410,764 | 5% | ||
bambugrove | 0 | 545,334,997 | 100% | ||
money-way | 0 | 580,680,092 | 100% | ||
angelslake | 0 | 8,533,753,465 | 5% | ||
yestermorrow | 0 | 8,858,780,521 | 31% | ||
dalz | 0 | 7,638,276,629 | 10% | ||
smartvote | 0 | 189,072,386,889 | 4.5% | ||
alijewel | 0 | 568,579,240 | 100% | ||
blues-wclouds | 0 | 1,752,009,336 | 100% | ||
dlike | 0 | 75,562,997,659 | 2.8% | ||
triptolemus | 0 | 14,732,349,949 | 14.8% | ||
voxmortis | 0 | 52,861,948,800 | 20% | ||
wordit | 0 | 8,265,609,640 | 5% | ||
munhenhos | 0 | 1,935,344,360 | 20% | ||
sj-jeong | 0 | 266,184,627,564 | 5% | ||
wondumyungga | 0 | 18,304,063,047 | 5% | ||
bobby.madagascar | 0 | 10,424,121,604 | 7.4% | ||
aicurator | 0 | 1,156,329,609 | 30% | ||
aquawink | 0 | 21,358,924,196 | 5% | ||
lkvictor2005 | 0 | 21,557,969,918 | 100% | ||
jamzmie | 0 | 927,812,377 | 15% | ||
sasaadrian | 0 | 32,140,469,029 | 20% | ||
reverseacid | 0 | 772,911,315 | 5% | ||
pollygonewild | 0 | 0 | 10% | ||
ringit | 0 | 14,072,704,210 | 5% | ||
new-steemit | 0 | 5,695,873,588 | 10% | ||
coreabeforekorea | 0 | 173,935,367,581 | 5% | ||
talkative-bk | 0 | 213,334,531,284 | 5% | ||
starrouge | 0 | 1,048,492,524 | 50% | ||
freepress | 0 | 39,085,867,294 | 25% | ||
zerofive | 0 | 826,217,444 | 50% | ||
primeradue | 0 | 5,192,010,829 | 14.8% | ||
delabo | 0 | 83,253,716,222 | 30% | ||
steemegg | 0 | 763,473,562 | 2.5% | ||
tagalong | 0 | 1,839,508,748 | 100% | ||
cnstm | 0 | 977,760,614,197 | 100% | ||
arhitekto | 0 | 495,636,541 | 50% | ||
permaculturedude | 0 | 2,921,230,284 | 7.4% | ||
likuang007 | 0 | 39,674,343,456 | 100% | ||
ctime | 0 | 593,320,498,295 | 8% | ||
lianjingmedia | 0 | 1,004,005,509 | 100% | ||
bobdavids | 0 | 0 | 10% | ||
a-secondchance | 0 | 1,238,265,549 | 10% | ||
chocolatelover | 0 | 3,997,662,554 | 40% | ||
instagram-models | 0 | 23,234,600,897 | 4% | ||
mia-cc | 0 | 5,495,145,610 | 100% | ||
cecilian | 0 | 9,258,508,432 | 99.56% | ||
hungrybear | 0 | 115,149,568 | 0.5% | ||
fintian | 0 | 535,809,177 | 100% | ||
electrodo | 0 | 74,962,500,734 | 10% | ||
smileyboy | 0 | 44,197,781,980 | 30% | ||
hungryharish | 0 | 5,096,573,944 | 100% | ||
elektroyazilim | 0 | 627,718,393 | 60% | ||
kryptogames | 0 | 105,603,741,174 | 20% | ||
lovelemon | 0 | 60,680,867,419 | 100% | ||
theinspiration | 0 | 550,036,013 | 100% | ||
epic4chris | 0 | 553,592,953 | 100% | ||
bergelmirsenpai | 0 | 2,602,145,808 | 30% | ||
rj-photo | 0 | 1,421,353,644 | 100% | ||
blockchainpeople | 0 | 17,381,447 | 3% | ||
whatdidshewear | 0 | 32,497,044,845 | 5% | ||
sjgod4018 | 0 | 102,982,904,346 | 5% | ||
mustard-seed | 0 | 16,039,640,851 | 5% | ||
morwen | 0 | 18,282,083,049 | 11% | ||
likwid | 0 | 4,699,596,463,499 | 25% | ||
kpopprincess | 0 | 171,969,440 | 50% | ||
map10k | 0 | 14,646,597,702 | 14% | ||
moonhodler | 0 | 1,961,336,253 | 20% | ||
soloduet | 0 | 167,502,640 | 50% | ||
steem.leo | 0 | 70,588,191,432 | 14.65% | ||
leo.voter | 0 | 104,259,413,416 | 14.8% | ||
hyborian-strain | 0 | 1,965,101,272 | 30% | ||
perrymine | 0 | 928,073,657 | 5% | ||
freddio.sport | 0 | 1,075,829,477 | 15% | ||
asteroids | 0 | 979,869,909 | 14.8% | ||
mapxv | 0 | 16,868,880,860 | 14% | ||
botante | 0 | 70,937,358,789 | 30% | ||
wenjiewu | 0 | 98,613,800 | 25% | ||
leo.syndication | 0 | 2,673,448,570 | 14.8% | ||
one.life | 0 | 10,366,822,544 | 14.77% | ||
maxuva | 0 | 636,638,501 | 13.2% | ||
maxuvb | 0 | 675,008,249 | 13.2% | ||
maxuvd | 0 | 6,382,384,653 | 14.8% | ||
maxuve | 0 | 52,550,910,469 | 10.8% | ||
teresa.yaiyai | 0 | 8,142,979,703 | 100% | ||
viktorv | 0 | 576,284,508 | 51% | ||
freedomteam2019 | 0 | 2,878,482,697 | 25% | ||
jaykayw | 0 | 11,444,063,045 | 5% | ||
stevoperon | 0 | 665,610,816,961 | 40% | ||
sacrosanct | 0 | 1,390,343,916 | 14.06% | ||
friendlystranger | 0 | 270,302,621,928 | 16% | ||
starnote | 0 | 217,496,654 | 88% | ||
reward.tier1 | 0 | 574,707,399 | 100% | ||
spx | 0 | 105,362,107,459 | 100% | ||
oblivioncubed | 0 | 547,553,821 | 10% | ||
simply2koool | 0 | 10,599,120,172 | 25% | ||
razer7 | 0 | 280,948,909,661 | 28% | ||
moochain.net | 0 | 219,209,189 | 88% | ||
moochain.com | 0 | 0 | 88% | ||
bettyliu | 0 | 0 | 88% | ||
madeehaali | 0 | 536,195,211 | 100% | ||
bcm.leo | 0 | 2,322,797,380 | 7.4% | ||
steemcityrewards | 0 | 36,468,794,629 | 14.8% | ||
lovequeen | 0 | 5,223,594,446 | 100% | ||
dev.supporters | 0 | 53,131,485,552 | 10% | ||
steemzh | 0 | 0 | 88% |