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

标签 python python-3.x subprocess python-3.6

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/

相关文章:

python - 有没有更自动化的方法来发现子字符串?

Python PIL int 对象不可下标

python - 如何将正则表达式与 pandas series.find 函数一起使用

Python 子进程引用导致 fd 耗尽

python - 解包、扩展解包和嵌套扩展解包

python - 将多个函数应用于数组的每一行

python - 如何在 Python 中使用 Google Shortener API

python - 将缺少加号偏移日期时间字符串转换为正确的日期时间

python - 使用 Subprocess、Python 和 PSQL 进行多处理

Python子进程返回代码无需等待