python - 将 subprocess.Popen (shell=True) 与 Windows 文件夹一起使用

标签 python windows python-3.x subprocess popen

我目前正在研究 Popen 来自动压缩和存储文档。

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

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

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

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

最佳答案

在 Windows 中,您应该对文件或目录名使用引号(如果您想在其中使用空格)。在 Python 中,您应该使用\符号转义引号(如果您在 "引号内使用字符串)。如下所示:

"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)

关于python - 将 subprocess.Popen (shell=True) 与 Windows 文件夹一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40877625/

相关文章:

java - 在 jython 中使用 jcifs 以便使用 NTLM 安全性访问站点

python - Spark RDD - 使用额外参数进行映射

python - 如何直接在 GPU 或另一个张量的设备上创建张量?

python - 导入错误 : cannot import name 'languages'

python - 使用 Python 3 的 readlines() 处理 Unicode 错误

python - tensorflow 相对于 numpy 的一般计算优势

windows - Cygwin 错误 : "child_info_fork::abort: Loaded to different address:"

windows - 运行 32 位 Python 2.7 构建的 64 位 Windows 10 的 python-magic 安装挑战

c++ - 无法访问注册表项

python - 使用 Pandas 计算不同子段的 T 统计量