私はプログラム間でやりとりする必要があるいくつかの変数を追跡するためにconfigパーサーを使用しようとしています。 (私はこれがConfigParserのためのものであるとは確信していませんが、私が入手したときにプログラムがどのように働いたのかはまだ分かっています)。いくつかの空白行を追加して、その直前の行の最後の数文字をテキストで(あるいは以前にそこにあったテキストから)繰り返します。正直なところ、以下のコードを実行するだけで簡単に理解できます。ConfigParserが意図せず余分な行と文字を追加する
いずれにしても、私はウェブを検索してこの問題の言及を見つけることができませんでした。どのようなアイデアが間違っている?私はあきらめて別の図書館を試してみるべきですか?
ご協力いただきありがとうございます。
システム:Windows(ユーザーへの変更<>ユーザー)エラーを再現するためのPython 2.7.10
コードを使用して:
from ConfigParser import ConfigParser
import os
def create_batch_file(name, config_dir, platform, ABCD, batch_ID, speed, parallel, turbo=False):
## setup batch_info.ini
batch_ini = os.path.join(config_dir, "batch_info.ini")
# Cast to string in case they are none object
batch_ID = str(batch_ID).lower()
parallel = str(parallel).lower()
if parallel == "none":
parallel = 1
batch_ini_contents = ["[Machine]\n",
"name = {}\n".format(name),
"platform = {}\n".format(platform),
"\n",
"[Environment]\n",
"turbo = {}\n".format(turbo),
"speed = {}\n".format(speed),
"\n",
"[Batch]\n",
"batch_id = {}\n".format(batch_ID),
"ABCD = {}\n".format(ABCD),
"parallel = {}\n".format(parallel),
"rerun = False\n",
"\n"
"[Reservation]\n"
"reserved = False\n"
"purpose = None"
]
with open(batch_ini, 'w+') as f:
f.writelines(batch_ini_contents)
return batch_ini
def temp_getter(config_dir):
config = config_dir
fl = ConfigParser()
fl.read(config)
return fl
config_dir = "C:\\Users\\<user>\\Desktop"
batch_ini = os.path.join(config_dir, "batch_info.ini")
name = "m"
platform = "p"
turbo = False
speed = "default"
batch_ID = 5
ABCD = 'abcd'
parallel = 1
purpose = "hello hello hello"
create_batch_file(config_dir=config_dir, ABCD=ABCD, turbo=turbo,
batch_ID=batch_ID, parallel=parallel, speed=speed, name=name,
platform=platform)
f = temp_getter(batch_ini)
f.set('Reservation', 'reserved', 'True')
f.set('Reservation', 'purpose', purpose)
with open(batch_ini, 'r+') as conf:
f.write(conf)
f = temp_getter(batch_ini)
f.set('Reservation', 'reserved', 'False')
f.set('Reservation', 'purpose', 'None')
with open(batch_ini, 'r+') as conf:
f.write(conf)
昨日何らかの理由で私がw +を使用していたときに空白のファイルが残っていたので、r +に切り替えました。しかし、あなたは正しい、w +への切り替えは問題を解決するようだ。私はトラブルシューティング中に別のものを変更したに違いありません。いずれにせよ、ありがとうございました!本当にありがとう! – Shmuelt