I have a python file named constants.py
with the following contents
from pytestLoop.config.testdata_005_connect_for_bank_statements import *
xpath_username="//div[text()='"+uname+"']"
pswd="//div[@id='password']"
# this is the end of the file.
I have another python file called alterConstants.py
with the following contents
def changeConstants(fname):
f = open("pytestLoop/config/constants.py",'r+')
line = f.read()
text=(line[line.index('config.') + 7:line.index(' import')])
linenew = line.replace(text, fname)
f.seek(0)
f.write(linenew)
f.close()
changeConstants("testdata_001")
after i execute the above alterConstants.py
file, this is the content of constants.py
from pytestLoop.config.testdata_001 import *
xpath_username="//div[text()='"+uname+"']"
pswd="//div[@id='password']"
# this is the end of the file.his is the end of the file.
my initial goal is to change the line from pytestLoop.config.testdata_005_connect_for_ban_statementsimport *
to import whatever filename I pass in the changeConstants
function in alterConstants.py
which is working fine
but python also writes some additional characters to the last line of the constants.py
file
- Can someone please explain why does this happen ?
- Also please suggest on How to fix this issue.
Thanks in advance.