将 subprocess.Popen (shell=True) 与 windows 文件夹一起使用 [英] Using subprocess.Popen (shell=True) with windows folders

查看:85
本文介绍了将 subprocess.Popen (shell=True) 与 windows 文件夹一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究 Popen 来自动化压缩和文件存储.

I'm currently looking at Popen to automate the compression & storage of documents.

对于压缩部分,我想到了以下 Python 行:

For the compression part, I thought of the following Python line:

subprocess.Popen("WinRAR.exe a -r c:\\03. Notes\\AllTexts *.txt", shell=True)

我一直收到错误消息,因为该命令无法处理包含空格的文件夹名称(03.Notes).这个问题之前被问过几次,而且,我必须说我尝试了所有的命题:原始字符串、双引号、三引号……它们都不起作用.

I keep having error messages, as the command is not able to deal with a folder name that contains a space (03. Notes). This question was asked before several times, and, I must say that I tried all propositions: raw string, double quotes, triple quotes, ... None of them worked.

由于我无法更改文件夹名称,而且我没有更多的想法可以尝试,有人可以建议我如何成功通过此命令行吗?

As I can't change the folder names, and I have no more ideas to try, could someone advise how could I possibly pass this command line successfully ?

推荐答案

在 Windows 中,您应该对文件名或目录名使用引号(如果您想在内部使用空格).在 Python 中,你应该用 \ 符号转义引号(如果你在 " 引号内使用字符串).像这样:

In Windows you should use quotes for file or directory names (if you want to use spaces inside). In Python, you should escape quotes with \ symbol (if you are using strings inside " quotes). Like this:

"my name is \"Mark\"!"

或者只是:

'my name is "Mark"!'

所以这将按预期工作:

subprocess.Popen("WinRAR.exe a -r \"c:\\03. Notes\\AllTexts\" *.txt", shell=True)

还有:

subprocess.Popen('WinRAR.exe a -r "c:\\03. Notes\\AllTexts" *.txt', shell=True)

这篇关于将 subprocess.Popen (shell=True) 与 windows 文件夹一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆