0

I have a python program that receives a file path as an argument.

The problem is, that if the file path has the following chars: "%cd", then it replaces "%cd" with the current directory.

So for example:

python program.py  "C:\%af%32%cd%7f.htm"

The sys.argv (in program.py) shows this:

['program.py', 'C:\\%af%32C:\\%7f.htm']

Why is that happening and how can it be solved?

Edit

The problem is that if I put "%cd" in the command line, then in python I get the command line with the current directory string instead the "%cd" chars

Drxxd
  • 1,860
  • 14
  • 34
  • 1
    This is not a python issue, but a Windows CMD one. Windows CMD replaces %cd% before passing it to python. – Ben Nov 01 '17 at 16:49
  • @Ben Maybe.. In the mean time I didn't find that "%cd" works in windows CMD.. But I found that "%cd" is a special command in ipython – Drxxd Nov 01 '17 at 16:52
  • On Unix-like systems you stop parameter expansions like that by quoting the arg with single quotes (not double quotes). Doesn't that work on Windows? – PM 2Ring Nov 01 '17 at 16:52
  • Use %% in the command line see https://stackoverflow.com/questions/2058944/how-do-you-suppress-environment-variable-expansion-within-dos#2058972 – Martin Beckett Nov 01 '17 at 16:53
  • @MartinBeckett Adding another % before each "%" character didn't work :( – Drxxd Nov 01 '17 at 16:57
  • #Drxxd - yes sorry that only works inside .bat scripts. See my answer below. From https://stackoverflow.com/questions/33016094/is-there-a-way-to-prevent-percent-expansion-of-env-variable-in-windows-command-l – Martin Beckett Nov 01 '17 at 17:07

1 Answers1

2

Escaping % with %% only works within batch files, not on the commandline. It is possible to escape % with double quote

eg: python untitled0.py "C:\\"%"af"%"32"%"cd"%"7f.htm"

['untitled0.py', 'C:\"%af%32%cd%7f.htm']

But this is a bit tedious - and you have to be careful of \ escaping any of the ".

Another alternative would be to replace % with some other symbol and then change it back inside the python program?

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • You forgot to remove the double quote from the output. – Eryk Sun Nov 01 '17 at 17:43
  • Note that handling the quotes on the command line is a two-part process. First, quoting prevents CMD from interpreting special characters (e.g. "&"), but CMD passes the quotes on the command line exactly as written. It's up to the child process to remove them, which is usually via the C runtime, which parses the command line into the `argv` list. [`CommandLineToArgvW`](https://msdn.microsoft.com/en-us/library/bb776391) follows similar rules. – Eryk Sun Nov 01 '17 at 17:43
  • @eryksun it looks like the python interpreter strips them. That was the output of just print(sys.argv) in python – Martin Beckett Nov 01 '17 at 18:12
  • Yes, Python gets `argv` from the C runtime, but I wanted to clarify the role of the shell here for Unix users who might assume the shell handles removing the quotes and creating an `argv` array. Windows programs get a command line string, and it's up to each program how it should be parsed. – Eryk Sun Nov 01 '17 at 18:41