ファイル内の最初の文字を置き換えて、1
または0
のいずれかになりました。私のコードは次のとおりです。ファイルの最初の文字を置換するPython 2.7
import os
hostName = raw_input("IP: ")
communicate = open("communicate.txt", "w")
while True:
response = os.system("ping " + hostName + " -c 1")
if response == 0:
# Replace first character with '1'
else:
# Replace first character with '0'
私はこのコードを、Linux仮想マシンで端末から実行しています。
ファイルを '" w "'で開くと、すでに上書きされています。 –
'communications.txt'ファイルは空ですか?そうでない場合は、内容を保存しますか?(最初の文字を除く) –