ファイルから行のブロックを削除する方法については、私は苦労しています。コードは以下の通りですPythonファイルから行のブロックを削除する
#!/usr/bin/python
import argparse
import re
import string
##getting user inputs
p = argparse.ArgumentParser()
p.add_argument("input", help="input the data in format ip:port:name", nargs='*')
args = p.parse_args()
kkk_list = args.input
def printInFormat(ip, port, name):
formattedText = '''HOST Address:{ip}:PORT:{port}
mode tcp
bind {ip}:{port} name {name}'''.format(ip=ip,
port=port,
name=name)
textWithoutExtraWhitespaces = '\n'.join([line.strip() for line in formattedText.splitlines()])
# you can break above thing
# text = ""
# for line in formattedText.splitlines():
# text += line.strip()
# text += "\n"
return(formattedText)
#####here im writing writing the user inoput to a file and it works great.
#with open("file.txt", "a") as myfile:
# for kkk in kkk_list:
# ip, port, name = re.split(":|,", kkk)
# myfile.write(printInFormat(ip, port, name))
###### here is where im struggling.
for kkk in kkk_list:
ip, port, name = re.split(":|,", kkk)
tobedel = printInFormat(ip, port, name)
f = open("file.txt", "r+")
d = f.readlines()
f.seek(0)
if kkk != "tobedel":
f.write(YY)
f.truncate()
f.close()
ご覧のとおり、私はfile.txtにユーザー入力を追加しています。すなわち(形式:ip:port:name)。スクリプトは今、私は、ファイルからの行(複数可)を削除したい./script.pyが
Host Address:192.168.0.10:PORT:80
mode tcp
bind 192.168.0.10:80 abc
Host Address:10.1.1.10:PORT:443
mode tcp
bind 10.1.1.10:443 xyz
を192.168.0.10:80:string 192.168.0.10:80:stringとしてexcutedされている場合、ファイルは、エントリの下に含まれています。ユーザ入力が同じように与えられたときには、txt。上記のコードを実行すると何も起こりません。私は初心者であり、あなたが私の理解を助けるならば本当に気持ち良いです。この質問は、python multiple user args
何を削除しますか?あなたは少し説明できますか?同じ例があるかもしれません。あなたのコードからはそれほど明確ではありません。 – arunk2
@ArunKumar 2番目のコードウィンドウに記載されている2つのブロックを削除しようとしています。スクリプトがargs::で実行されると、対応するエントリがfile.txtから削除されます。ありがとう。 –
bindo