Answer the question
In order to leave comments, you need to log in
How to write to a file after a certain method?
Hey! I can’t solve the problem here, tell me how to make it so that Python writes text to a file with code, but in a certain method?
here is one
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.animation import Animation
from kivy.clock import Clock
from kivy.config import Config
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.core import audio
from kivy.core.audio import SoundLoader
#import socket
#sock = socket.socket()
#sock.bind(('', 9090))
#sock.listen(1)
Config.set('graphics', 'resizable', '0')
Config.set('graphics', 'width', '1280')
Config.set('graphics', 'height', '720')
class MainApp(App):
def build(self):
main_layout = FloatLayout()
return main_layout
if __name__ == '__main__':
app = MainApp()
app.run()
def build(self):
main_layout = FloatLayout()
#тут
Answer the question
In order to leave comments, you need to log in
According to the initial condition, you can write regexp or "state machine" with the hardcode of the file content, but if the content changes, the code will have to be rewritten. The option below is simpler, but it differs from the original condition.
If there is no restriction on the file format and the code must always be entered in the same type of places, you can use the jinja2 template module or placeholders for working with strings, like:
your_new_code="""
print(123)
return False
"""
...
updated_text = """
...
class MainApp(App):
def build(self):
main_layout = FloatLayout()
{some_text}
...
""".format(some_text=your_new_code)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question