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 因为它无法复制文件。我已经解决了,但我想知道是否有人知道为什么会这样?任何信息或链接将不胜感激。
最佳答案
返回0为成功。不为 0 是失败。返回码可以是从 0 到 4294967295 的 32 位整数.看Exit status基于操作系统。
根据 Python 3 文档的确认,有时可能会设置或收到负返回码。
Popen.returncode
The child return code, set by poll() and wait() (and indirectly by communicate()). A None value indicates that the process hasn’t terminated yet.
A negative value -N indicates that the child was terminated by signal N (POSIX only).
在 Windows 上使用 exit(-1)
退出 Python 会导致 4294967295
,因此 Python 使用无符号的 32 位返回代码。
关于python - 错误 Python 子进程调用复制文件。没有文件,失败,但返回 1。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48017009/