Reputation: 53
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)
So I am copying a file from one directory to another. When I run the subprocess.call() method it returns a 1. Except that nothing happens and I get the following message on my terminal.
cp: /Users/my_name/Desktop/python/copy_files/copy_me_test.txt: No such file or directory
1
What's going on here? Shouldn't this return a 0 since it failed to make a copy of the file. I made a work around already but I would like to know if anyone knows why this is happening? Any information or links would be appreciated.
Upvotes: 5
Views: 1256
Reputation: 5372
Return 0 is success. Not 0 is failure. Return code can be a 32 bit integer from 0 to 4294967295. Look at Exit status based on operating system.
Sometimes a negative return code may be set or received as Python 3 docs confirm.
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).
Exit Python with exit(-1)
results in 4294967295
on Windows so Python uses a unsigned 32 bit return code.
Upvotes: 3
Reputation: 16214
According to the documentation here https://docs.python.org/2/library/subprocess.html it will return 1 when it fails, also check that there is a warning if you use the attribute shell=True
Upvotes: 2