Python Forum
add Escape charcters in string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
add Escape charcters in string
#1
Hi
I am trying to call an application from a python script in an Azure Devops pipeline
I build a string to the app path
cmd = "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
The problem is that I need to escape \ with \\ in order to get it run
Have tried
cmd = cmd.replace("\","\\")
but this gives an error SyntaxError: unexpected character after line continuation character
How can I achieve this?
Thanks
Reply
#2
Well if you want to play save and have it for every platform you can do this:
import os
path_sep = os.path.sep
then you can create your first path with the system path separators
Reply
#3
And two more solutions:
  1. Make it a raw string by prepending "r". In raw strings a backlslash will not be interpreted.
    cmd = r"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
  2. Use forward slashes instead of backslashes. Python will understand this.
Reply
#4
Thanks for the replies
I now have the correct string (I think)
But when run as part of an Azure Devops pipeline I get permission denied when calling subprocess?
Here is my code
              programFiles = os.environ['PROGRAMFILES(x86)'] + os.sep
              cmd = '"' + programFiles + 'Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow/vstest.console.exe' + '"'
              path = Path(cmd)
              print (path)
              subprocess.run([path,
                              filepath,
                              '/Logger:trx;LogFileName=' + fn + '.trx',
                              '/ResultsDirectory:' + resultsFolder
                              ])
Replacing path with a hardcoded string this succeeds - any ideas
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  use of escape character in re.sub and find WJSwan 1 876 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  Escape indentation Frankduc 11 2,978 Jan-31-2022, 02:41 PM
Last Post: Frankduc
  Escape Single quotation between each content tag usman 3 2,742 May-02-2021, 03:32 PM
Last Post: snippsat
  DIY Escape Room for fun StannemanPython 1 2,270 Feb-17-2021, 10:53 PM
Last Post: maurom82
  How to escape OrderedDict as an argument? Mark17 2 1,990 Dec-23-2020, 06:47 PM
Last Post: Mark17
  Trying to cycle through a list of charcters pooky2483 12 4,316 Sep-28-2020, 06:55 AM
Last Post: pooky2483
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,420 May-15-2020, 01:37 PM
Last Post: snippsat
  help for escape sequences NewPi 1 1,997 Dec-11-2019, 11:22 PM
Last Post: ichabod801
  escape single quote deep_logic 1 1,766 Sep-10-2019, 08:05 PM
Last Post: SheeppOSU
  The use of escape char \ hishamzero1 2 2,340 Aug-12-2019, 10:20 PM
Last Post: hishamzero1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020