错误Python子进程调用复制文件。没有文件,失败,但返回1.为什么?

问题描述 投票:3回答:2
import subprocess
import os

#files
src_file = '/../copy_me_test.txt'
destination_file = 'paste_here.txt'

#make copy of file
shell_command = 'cp "%s" "%s"' % (src_file, destination_file)
successful = subprocess.call(shell_command, shell = True)
print(successful)

所以我将文件从一个目录复制到另一个目录。当我运行subprocess.call()方法时,它返回1.除了没有任何反应,我在终端上收到以下消息。

cp: /Users/my_name/Desktop/python/copy_files/copy_me_test.txt: No such file or directory
1

这里发生了什么?不应该返回0,因为它无法复制文件。我已经做了一个工作,但我想知道是否有人知道为什么会这样?任何信息或链接将不胜感激。

python python-3.x subprocess python-3.6
2个回答
2
投票

返回0成功。不是0就是失败。返回码可以是从0到4294967295的32位整数。根据操作系统查看Exit status

有时可以设置或接收否定返回代码,因为Python 3文档确认。

Popen.returncode

子返回码,由poll()和wait()设置(间接由communic()设置)。 “无”值表示该进程尚未终止。

负值-N表示孩子被信号N(仅POSIX)终止。

使用exit(-1)退出Python导致Windows上的4294967295,因此Python使用无符号的32位返回码。


1
投票

根据这里的文档https://docs.python.org/2/library/subprocess.html它会在失败时返回1,如果你使用属性shell = True也会检查是否有警告

© www.soinside.com 2019 - 2024. All rights reserved.