put text in a file in one line in python

You can put text in a file in one line by first opening the file in write mode and then writing the text to the file using the write() method. Here's an example:

main.py
# Open file in write mode
with open('filename.txt', 'w') as file:
    # Write text to file
    file.write('This is some text in one line')
139 chars
5 lines

In this example, we first open a file named filename.txt in w or write mode using a with statement (which automatically closes the file when we're done with it). We then use the write() method to write the text 'This is some text in one line' to the file.

Note that if the file already exists, opening it in write mode will overwrite its contents. If you want to add text to an existing file without overwriting its contents, you can open it in append mode by using 'a' instead of 'w'.

gistlibby LogSnag