Python Forum
[SOLVED] Linefeed when writing "f" strings to text file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Linefeed when writing "f" strings to text file?
#1
Question 
Hello,

I don't know how those strings are called in Python.

In the second block, I can't get linefeeds: The two strings are on a single line. What's the correct syntax?

#OK with open("test.txt", 'w',encoding='utf-8') as writer:	writer.write("line1")	writer.write("line2\r\n")	writer.write("line3\n")	writer.write("line4") #NOK #with open(f"{TITLE}.cmd", 'wt',encoding='utf-8') as writer: with open(f"{TITLE}.cmd", 'w',encoding='utf-8') as writer:	writer.write(fr"c:\test -f 139 {URL}\\n")	writer.write(fr"c:\test -f 92 {URL}\\n")
Thank you.

---
Edit: Makes no difference:

with open(f"{FILE}.cmd", 'wt',encoding='utf-8') as writer:	writer.write(f"{TITLE}\n")	writer.flush()	writer.write(fr"c:\blah -o audio.m4a -f 139 {URL}\\n")	writer.flush()
Reply
#2
Not too sure exactly what you are trying to achieve. Maybe use print()?

title = '/home/peterr/temp/TITLE' url = '123.456.789.123' with open(f"{title}.cmd", 'w') as writer: print(fr"c:\test -f 139 {url}", file=writer) print(fr"c:\test -f 92 {url}", file=writer)
Reply
#3
Just writing to a file.

Isn't this the right way to do it?

with open("blah.txt", 'wt',encoding='utf-8') as writer:	writer.write("line1")	writer.write("line2")
---
Edit: The problem occurs specifically when using the "fr" setting:

TITLE = "blah" URL = "blah2" with open('readme.txt', 'w') as f:	#BAD	f.write(fr"c:\blah -o audio.m4a -f 139 {URL}\\n")	#BAD	f.write(fr"c:\blah -o audio.m4a -f 139 {URL}\n")	f.write(f"{TITLE}\n")	f.write('line1\n')	f.write('line2\n')
---
Edit: A work-around is to simply use forward slashes. Windows is fine with that syntax:
with open(f"{FILE}.cmd", 'wt',encoding='utf-8') as writer:	writer.write(f"c:/blah -o audio.m4a -f 139 {URL}\n")
Reply
#4
Best to show the complete runnable module, or at least enough so that the code can be run and tested by us.

It looks as though you are trying to make it too complicated.

take a look at Python 3 Module of the Week

specifically The File System

It will get you going in the right direction.
Reply
#5
Thanks for the links.

Here's a snippet that shows the problem:

FILE = "blah" with open(f"{FILE}.cmd", 'wt',encoding='utf-8') as writer:	writer.write(f"REM {FILE}\n")	#r prefix to use back slashes instead of forward slashes	#no newline	writer.write(rf"c:\{FILE}.exe dummy\n")	writer.write("dummy")
Reply
#6
The problem is not being able to use new-line \n inside raw string. So two possible solutions:

FILE = "blah" with open(f"{FILE}.txt", 'wt',encoding='utf-8') as writer: writer.write(f"REM {FILE}\n") writer.write((rf"c:\{FILE}.exe dummy" "\n")) # use brackets to concatenate 2 string literals writer.write("dummy")
or

FILE = "blah" NEWLINE='\n' # define a constant for later use inside the f-string with open(f"{FILE}.txt", 'wt',encoding='utf-8') as writer: writer.write(f"REM {FILE}\n") writer.write(rf"c:\{FILE}.exe dummy{NEWLINE}") writer.write("dummy")
credit: https://stackoverflow.com/q/14974222/4046632

That said, you may prefer different approach to writing windows path, not using raw string
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  .xlsm file can't be opened after writing data to one worksheet mhyatt 4 720 Oct-11-2025, 11:25 PM
Last Post: Pedroski55
Question [SOLVED] Open file, and insert space in string? Winfried 7 2,460 May-28-2025, 07:56 AM
Last Post: Winfried
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 1,610 May-01-2025, 03:43 PM
Last Post: Winfried
  Problems writing a large text file in python Vilius 4 1,960 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  [SOLVED] [Linux] Write file and change owner? Winfried 6 3,129 Oct-17-2024, 01:15 AM
Last Post: Winfried
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 4,037 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
  writing list to csv file problem jacksfrustration 5 3,963 Jul-04-2024, 08:15 PM
Last Post: deanhystad
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 16,272 Feb-29-2024, 12:30 AM
Last Post: Winfried
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 1,898 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Trying to understand strings and lists of strings Konstantin23 2 2,366 Aug-06-2023, 11:42 AM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.