\n is treated as \ and n [duplicate]

2024/10/6 9:29:32

The following python code

env.Command(versionFile, allSrcs + [".git/index", "SConstruct"],'echo "#define ZSIM_BUILDDATE \\""`date`\\""\\\\n#define ZSIM_BUILDVERSION \\""`python misc/gitver.py`\\""" >>' + versionFile)

produces an output like this

$ cat build/opt/version.h
#define ZSIM_BUILDDATE "Sat Apr 19 13:31:41 CET 2014"\n#define ZSIM_BUILDVERSION "master:10:a8c417b:2fc 3+ 2- d5cec7e7"

As you can see it doesn't understand that '\n' means new line+carriage return. Instead it just print '\' and 'n'.

How can I fix that?

P.S: This question is a follow-up for this one. the previous post was general and didn't pinpoint to the problem. Also I use scons for build. any help would be appreciated. thank you

Answer

That's not actually either a python or a C++ question as you are complaining about the behavior of shell quoting (some shells allow escape codes like \n in arguments when you use something like echo -e though).

So if you want to get a newline in, try producing it in Python already (newline inside of quoted strings will make it into the argument of echo) rather than producing some escape sequence that the shell will not further process.

https://en.xdnf.cn/q/120472.html

Related Q&A

How to locate an element and extract required text with Selenium and Python

I am using Selenium to enter data on a web page, but have run into an issue with one of the input fields. This is the HTML code causing my difficulty:<div class="form-group"> <label …

Determine type of disk on Windows with Python

I want to list all of my disk and can be able to know its type: "SATA" "NVME" "M.2" or "PCI" on Windows computer. I made some research with wmi and I get the int…

How to get a list the visible vertices and segments of a mesh

I work on pose estimation of a 3d objects. I am using CAD model of that object to generate all the possible hypothesis of its pose. I am using pyopengl to render the view of the object from a specific…

Python function calls the wrong method/target

The following program simulates a traffic light system with some buttons. The buttons appear correctly, but if Im trying to call the method to create/change the LEDs, it ends up in the wrong method. He…

How to make a triangle of xs in python?

How would I write a function that produces a triangle like this:xxxxxxxxxx xxxxxLets say the function is def triangle(n), the bottom row would have n amount of xsAll I know how to do is make a box:n = …

Pip freeze --local

I am following a video tutorial and that guy did this:$ pip freeze --local > requirement.txt $ cat requirement.txtthis is to export all these packages with their versions in another project, but how…

How to optimize this Pandas code to run faster

I have this code to create a swarmplot from data from a DataFrame:df = pd.DataFrame({"Refined__Some_ID":some_id_list,"Refined_Age":age_list,"Name":name_list …

i was creating a REST api using flask and while i was about to test it on postman I saw that error

File "c:\Users\kally\rest\code\app.py", line 3, in <module>from flask_jwt import JWTFile "C:\Users\kally\AppData\Roaming\Python\Python310\site-packages\flask_jwt\__init__.py",…

Web scrape get drop-down menu data python

I am trying to get a list of all countries in the webpage https://www.nexmo.com/products/sms. I see the list is displayed in the drop-down. After inspecting the page, I tried the following code but I m…

TypeError(unsupported operand type(s) for ** or pow(): str and int,)

import mathA = input("Enter Wright in KG PLease :") B = input("Enter Height in Meters Please :")while (any(x.isalpha() for x in A)):print("No Letters Please")A = input(&qu…