How to enter escape character in a positional string argument from the command line?

Jach Feng jfong at ms4.hinet.net
Mon Dec 19 21:24:49 EST 2022


Mark Bourne 在 2022年12月20日 星期二凌晨4:49:13 [UTC+8] 的信中寫道:
> Jach Feng wrote: 
> > I have a script using the argparse module. I want to enter the string "step\x0A" as one of its positional arguments. I expect this string has a length of 5, but it gives 8. Obviously the escape character didn't function correctly. How to do it?
> That depends on the command-line shell you're calling your script from. 
> 
> In bash, you can include a newline in a quoted string: 
> ./your_script 'step 
> ' 
> (the closing quote is on the next line) 
> 
> Or if you want to do it on a single line (or use other escape 
> sequences), you can use e.g.: 
> ./your_script $'step\x0a' 
> (dollar sign before a single-quoted string which contains escape sequences) 
> 
> -- 
> Mark.
That's really good for Linux user! How about Windows?


More information about the Python-list mailing list