简体   繁体   中英

How to Print a Path in Python?

I want to make a script open the CMD and then enter a path:

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?

What I want it to enter is 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). Here is a short console session (python 2.7) showing that functionality:

>>> 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. when I want the output format, I call print to execute any formatting (escapes) contained within the string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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