Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python file.write() is adding an extra newline after a variable in a concatenated string

I have been trying to solve this issue. I have written:

file.write("pyautogui.write(" + "'" + textEntry + "'" + ")")

but in the file that is written to, the following is written:

pyautogui.write('test
')

I want it all to be on one line. Does anyone know the cause for this? I have tried fixing it, but to no avail.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

What’s happening here is that the textEntry variable likely has a \n character at the end of it, a simple way to solve that is by using strip(). Also, it is generally recommended to use f-Strings instead of doing the + each time. A solution is as follows:

file.write(f"pyautogui.write('{textEntry.strip()}')")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading