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

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会在Windows上导致4294967295,因此Python使用无符号的32位返回码.

上一篇:如何通过Python子进程杀死omxplayer


下一篇:python-在subprocess.Popen中,bufsize应用于哪个文件?