简体   繁体   English

将参数传递给python程序

[英]Passing arguments to a python program

I have a python program that receives a file path as an argument. 我有一个python程序,它接收文件路径作为参数。

The problem is, that if the file path has the following chars: "%cd", then it replaces "%cd" with the current directory. 问题是,如果文件路径具有以下字符:“%cd”,则它将“%cd”替换为当前目录。

So for example: 因此,例如:

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

The sys.argv (in program.py) shows this: sys.argv(在program.py中)显示此信息:

['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 问题是,如果我在命令行中输入“%cd”,那么在python中,我将获得带有当前目录字符串的命令行,而不是“%cd”字符

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" 例如: 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? 另一种选择是将%替换为其他符号,然后将其改回到python程序中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM