[英]How to Print a Path in Python?
I want to make a script open the CMD and then enter a path: 我想制作一个脚本来打开CMD,然后输入路径:
import pyautogui as pag
pag.hotkey('win','r')
pag.typewrite('cmd')
pag.press('enter')
pag.typewrite('C:\Users\XY\AppData\')
that doesn't work. 那行不通。 So, I tried this:
所以,我尝试了这个:
import pyautogui as pag
pag.hotkey('win','r')
pag.typewrite('cmd')
pag.press('enter')
pag.typewrite('C:\\Users\\huba5_000\\AppData\\')
However, this entered C:?Users?XY?AppData?
但是,这输入了
C:?Users?XY?AppData?
What I want it to enter is C:\\Users\\XY\\AppData\\
. 我要输入的是
C:\\Users\\XY\\AppData\\
。 Do you know what I should write instead of '\\\\' ? 您知道我应该写什么而不是'\\\\'吗?
Thank you in advance! 先感谢您!
when a string is read in from input()
or from text boxes in gui's (in general.. idk about pag) the extra slashes are automatically put in. They are not automatically put in for string literals in your code however and must be escaped (hence the double slash). 当从
input()
或gui的文本框中读取字符串时(通常.. idk关于pag),多余的斜杠将自动插入。它们不会自动插入代码中的字符串文字,但是必须转义(因此为双斜杠)。 Here is a short console session (python 2.7) showing that functionality: 这是一个简短的控制台会话(python 2.7),显示了该功能:
>>> s = raw_input('enter a path: ') #change raw_input to input() for python 3.x
enter a path: \usr\var
>>> s
'\\usr\\var'
>>> print s
\usr\var
notice when I entered the path, I did not escape my backslashes, yet when I call the internal representation of s
they have been put in for me. 请注意,当我进入路径时,我没有转义反斜线,但是当我调用
s
的内部表示时,它们已经为我放置了。 when I want the output format, I call print to execute any formatting (escapes) contained within the string 当我想要输出格式时,我调用print执行字符串中包含的所有格式(转义符)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.