- 如何檢查文件是否存在?
- 如何將文本附加到文件?
我知道如何創建該文件,但在這種情況下,它會覆蓋所有的數據:在Python中追加文本到文件
import io
with open('text.txt', 'w', encoding='utf-8') as file:
file.write('text!')
在*nix
我可以這樣做:
#!/bin/sh
if [ -f text.txt ]
#If the file exists - append text
then echo 'text' >> text.txt;
#If the file doesn't exist - create it
else echo 'text' > text.txt;
fi;
'開放( '的text.txt', 'A',編碼= 'UTF-8')' – kev 2012-02-15 16:52:52
這似乎是Python的3.x的,因爲你使用'encoding'參數爲'open()'。對? – 2012-02-15 16:54:02
我用搜索,找不到答案! – tomas 2012-02-15 17:03:33