python_changetxt.py 418 B

12345678910111213
  1. def alter(file,old_str,new_str):
  2. file_data = ""
  3. with open(file, "r", encoding="utf-8") as f:
  4. for line in f:
  5. if old_str in line:
  6. line = line.replace(line,new_str)
  7. file_data += line
  8. with open(file,"w",encoding="utf-8") as f:
  9. f.write(file_data)
  10. f.close()
  11. code_pro = 'mycode'
  12. code = f"secret_code = '{code_pro}'"
  13. alter("/Users/xc/Desktop/conf.toml", "secret_code", code)