ユーザー入力に応じてスクリプト内の変数を変更する関数を作成しようとしています。ビルトインを使用して.tell()
比較する変数を特定し始めましたが、書き込み先の位置が少なくとも10バイトずれていますか?
#! /usr/bin/env python
import re
class file_input:
def __init__(self):
count = 0
change = raw_input('Input? ')
with open('/home/Downloads/FILES/adobe.py','a+') as f:
for line in f.readlines():
if re.findall('script_data', line):
count += 1
## i put in a count to ignore the first 'script_data' mentioned in the __init__ method ##
if change != line[13:] and count == 2:
## if the user-input is not the same, re-write that variable ##
pos = f.tell()
f.seek(pos)
## i checked the position here and its not where i would think it would be ##
print pos
print 'data not matched up, changing now...'
f.write(change)
print line[13:]
f.close()
if __name__ == '__main__':
file_input()
script_data = 'this is going to be some data...'
ファイルを確認しようとするとscript_data
、入力データが異なっていても変数は残っており、新しいデータは1行下になります。