Python Forum
How do I handle escape character in parameter arguments in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I handle escape character in parameter arguments in Python?
#1
Hi,
I have the issue that Python change the string value of my parameter arguments.

Here are my skript parameter:

"C:\Ordner 1\" "D:\test 2\"

import sys
if len(sys.argv) > 1:    
    print(sys.argv[1])
    print(sys.argv[2])
The result is:

C:\Users\Joss\AppData\Local\Programs\Python\Python37-32\python.exe E:/test.py "C:\Ordner 1\" D:\test" 2\"
C:\Ordner 1" D:\test
2"

As you can see the parameters are torn in the wrong place because the escape character was executed. Angry

What I would like to see should be:

C:\Ordner 1\
D:\test 2\

I think Python shouldn't change any input string and give me the raw-string !
Reply
#2
Python is not changing anything. The arguments arrive with the changes already made. The same thing would happen if you were calling a program written in any language. The changes are made by the shell you are using to enter the command.

The easy fix is use / instead of \ when typing the file paths.
JKR likes this post
Reply
#3
You can use double slashes before the quotes as shown below:

C:\Users\Joss\AppData\Local\Programs\Python\Python37-32\python.exe E:/test.py "C:\Ordner 1\\" D:\test" 2\\"
Reply
#4
Thumbs Up 
As I have now found out, it is not a Python problem.
Thx to @deanhystad for the hint! Clap
The console also works with escape characters, which means that the string is not passed on as I assumed.

A work around for me whould be the following: "C:\Ordner 1\ " "D:\test 2\ " ... a space between \ and "
Reply
#5
I don't think that is a workaround. \t is the escape sequence for a tab. I'm surprised that didn't cause a problem. I wonder if the shell also ignores \b (backspace), \n (newline) \f (form feed), etc...

Forget about the backslashes. Windows is fine with you using / instead of \ in a file path. Your command line using /.
Output:
python test.py C:/Ordner 1/ D:/test 2/
I don't know why you needed the trailing space.
Reply
#6
I just noticed a problem in your original post.
C:\Users\Joss\AppData\Local\Programs\Python\Python37-32\python.exe E:/test.py "C:\Ordner 1\" D:\test" 2\"
Even if the \" had not caused a problem, the quotes were still in the wrong place.
Reply
#7
The issue you're encountering is due to the interpretation of escape characters in command-line parameters. In command-line parameters, the backslash \ is typically used to escape special characters, causing the backslashes in your input paths to be interpreted as escape characters rather than regular characters. To obtain the raw path strings, you can use raw strings to represent the parameters.

In Python, you can create a raw string by prefixing the string with a lowercase "r". This instructs Python not to interpret escape characters in the string. For example:

import sys

if len(sys.argv) > 1:
path1 = r"C:\Ordner 1\"
path2 = r"D:\test 2\"
print(path1)
print(path2)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 416 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Python rule about the space character surrounding the equal sign ineuw 10 1,641 Sep-21-2023, 09:17 AM
Last Post: ineuw
  use of escape character in re.sub and find WJSwan 1 929 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 11,099 Dec-26-2022, 08:48 AM
Last Post: ibreeden
Question log.exception() without arguments in old Python versions? cthart 5 1,176 Nov-19-2022, 07:09 PM
Last Post: Gribouillis
  python call stored procedure with two parameter mg24 4 1,536 Sep-27-2022, 05:02 AM
Last Post: deanhystad
  Escape indentation Frankduc 11 3,083 Jan-31-2022, 02:41 PM
Last Post: Frankduc
  add Escape charcters in string GrahamL 3 1,186 Jan-20-2022, 01:15 PM
Last Post: GrahamL
  [solved] unexpected character after line continuation character paul18fr 4 3,432 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  python error: bad character range \|-t at position 12 faustineaiden 0 3,701 May-28-2021, 09:38 AM
Last Post: faustineaiden

Forum Jump:

User Panel Messages

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