1

I'm facing a strange situation..after setting a variable as a string, I try to print that variable and get the string with a caracter replaced by a special caracter..

file_version_cmd="wmic datafile where name='C:\\\Program Files (x86)\\\Citrix\\\ICA Client\\\4.1\\\wfcrun32.exe'"
print(file_version_cmd)

Output of the print is : wmic datafile where name='C:\Program Files (x86)\Citrix\ICA Client\♦.1\wfcrun32.exe'

"..4.1.." is replaced by "..♦.1.."

Thanks for your help

CSA75
  • 27
  • 5
  • 1
    If you want to escape a \, use \\ (two backslashes, not three as you did). Better, use a raw string: `name = r'C:\Program Files...'` so you don't need to escape them. – Thierry Lathuille Mar 28 '17 at 10:10
  • What do you want the output to be? File paths with backslashes and spaces can be a real headache to get right. In this case I assume you want to pass this command to a windows shell as well. – Håken Lid Mar 28 '17 at 10:54

1 Answers1

1

You have '\\\4.' in your string which created non-printable bytes for '\4' using \ooo octal value escape sequence. The digits are interpreted as octal numbers.

The default \xhh syntax is used to represent that value in hexadecimal.

You can rewrite your string something like this using raw string literal :

file_version_cmd = r"wmic datafile where name='C:\Program Files (x86)\Citrix\ICA Client\4.1\wfcrun32.exe'"

or

file_version_cmd = "wmic datafile where name='C:\\Program Files (x86)\\Citrix\\ICA Client\\4.1\\wfcrun32.exe'"

Both of the above code snippets will result in :

wmic datafile where name='C:\Program Files (x86)\Citrix\ICA Client\4.1\wfcrun32.exe'

Also, you can use forward slashes as well as most of the Windows APIs accept both types of slash separators in filename (If the filename path will be consumed by the python itself but may not work as expected otherwise) :

file_version_cmd = "wmic datafile where name='C:/Program Files (x86)/Citrix/ICA Client/4.1/wfcrun32.exe'"

You can refer to this documentation if you want to learn more about python lexical analyzer.

Satish Prakash Garg
  • 2,213
  • 2
  • 16
  • 25