.yml ファイル (テキスト ドキュメントとして開く) の 1 行を置き換えるツールを作成しようとしています。ファイルに書き込んでいますが、すべてを取り出して「固定」行を書き込みます。私のコードは、1 つのインスタンス/ファイルのサンプルと共に以下にあります。ファイル名は重要であり、変更できません。
import sys
from os import listdir
from os.path import isfile, join
onlyfiles = [ f for f in listdir() if isfile(f) and str(f)[-4:]==".yml" ] #Gets the file names, can be ANYTHING
for pos in range(1, len(onlyfiles)): #So it stops at the end of the List
with open(onlyfiles[pos], 'r') as a: #Lets open it make sure it is good
for line in a:
if line == " world: Trolling\n": # this is the target line to replace
line = line.replace(" world: Trolling", " world: world\n") #rework the line, though it wont write even if i change the R to W
with open(onlyfiles[pos], 'w') as repl: #Writes to the file, but gets rid of everything else
repl.write(line)
print(line)
変更されていないドキュメントのサンプル:
timestamps:
login: 1373913158118
lastteleport: 1373918169442
logout: 1373918539235
kits:
trusted: 1373052268213
ipAddress: 142.165.45.129
nickname: Erin
lastlocation:
world: Trolling
x: -890.6999999880791
y: 87.0
z: -764.6999999880791
yaw: 91.99242
pitch: 75.449974
homes:
e:
world: Trolling
x: 4653.2140183238
y: 64.0
z: 80.02726116652944
yaw: 192.08363
pitch: 66.29998
「トローリング」のインスタンスを「世界」に置き換えようとしています.100以上のファイルで、他には何も触れずに