简体   繁体   English

如何在Python中打印路径?

[英]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.

相关问题 如何打印实际最短路径 Dijkastra Python - How to print actual shortest path Dijkastra Python 如何使用 ebpf python 从打开的系统调用打印文件路径? - How to print the file path from an open syscall using ebpf python? 如何在 Python 的另一个文件中打印使用某个 function 的文件路径? - How to print the file path that uses the certain function in another file in Python? 如何在Python中打印文件列表的绝对路径? - How can I print the absolute path of a list of files in Python? 如何在文件路径中调用变量并在python 2.7中打印输出 - How to call a Variable into file-path and print the output in python 2.7 如何使用 python 在 windows 10 中打印当前壁纸路径? - how to print current wallpaper path in windows 10 using python? Python unittest:如何断言文件或文件夹的存在并在失败时打印路径? - Python unittest: How to assert the existence of a file or folder and print the path on failure? 如何打印我们刚刚写在另一个目录中的文本文件的路径并在Python中的每个文件中打印字符串 - How to print path of the textfile that we just write in another directory and print string in every file in Python 如何正确应用 RE 以从给定路径获取姓氏(文件或文件夹的)并将其打印在 Python 上? - How to correctly apply a RE for obtaining the last name (of a file or folder) from a given path and print it on Python? oracle 如何更新查询生成 csv 文件并通过 python 将结果打印或写入特定路径位置 - How can oracle update query generating a csv file and print or write the result in a particular path location through python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM