Subprocess.Popen system can't find the file - subprocess

I'm not sure why subprocess can't find my file. I keep getting this error
WindowsError: [Error 2] The system cannot find the file specified
Here is my code:
import subprocess
from subprocess import PIPE
file= r'C:\Users\NIA\Desktop\2.py'
x = 'python' + file + str(7) + str(8)
process = subprocess.Popen([x], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
if stderr:
print stderr
else:
print stdout

Related

Could not read from the file with contents of 'stdout=file_object' in python

I was trying to experiment with subprocess.Popen() constructor by using file object for the stdout. When I run the below code, the file gets created (with the contents) but contents are not displayed on the console. I cannot understand what I am missing. Can anyone please point out my mistake(if any)?
file_name = "abc.txt"
fo = open(file_name, 'w')
cmd = 'dir'
child = subprocess.Popen(cmd, stdout=fo, stderr=fo, shell=True)
fo.close()
with open(file_name, 'r') as fo: print(fo.read())
You have to wait for subprocess finish before reading the content.
#!/usr/bin/env python
import subprocess
file_name = "abc.txt"
fo = open(file_name, 'w')
cmd = 'dir'
child = subprocess.Popen(cmd, stdout=fo, stderr=fo, shell=True)
child.wait()
fo.flush()
fo.close()
with open(file_name) as fo:
print(fo.read())
# print (child.poll())

Popen samtools not running

I am writing a function of a class to convert a sam file to bam file, then sort it and index it in python (I know it is easy to fulfill in bash, but it does not hurt to learn more).
A sam file has already been generated in the defined directory (cwd) by another function of the class already.
Here is the code:
def sam2bam(self):
"""
return a sorted and index bam file in the working directory
"""
if str(self.fq)[7:9] == '24':
# create bam
comnd = "samtools view -bS " + str(self.fq)[0:10] + ".sam > " + str(self.fq)[0:10] + ".bam"
print("Converting sam to bam ... (executed command: {})\n".format(comnd))
comnd_input = shlex.split(comnd)
with open((str(self.fq)[0:10] + '.bam'), 'w') as f:
Popen(comnd_input, stdout = f, stderr = PIPE, cwd = 'G24/' + str(self.fq))
# sort bam
comnd2 = "samtools sort " + str(self.fq)[0:10] + ".bam -o " + str(self.fq)[0:10] + ".sorted.bam"
print('Sorting bam ... (executed command: {}\n)'.format(comnd2))
comnd_input2 = shlex.split(comnd2)
with open((str(self.fq)[0:10] + 'sorted.bam'), 'w') as f_sort:
Popen(comnd_input2, stdout = f_sort, stderr = PIPE, cwd = 'G24/' + str(self.fq))
# index bam
comnd3 = "samtools index " + str(self.fq)[0:10] + ".sorted.bam"
print('Indexing bam ... (executed command: {}\n)'.format(comnd3))
comnd_input3 = shlex.split(comnd3)
with open((str(self.fq)[0:10] + 'sorted.bam'), 'w') as f_index:
Popen(comnd_input3, stdout = f_index, stderr = PIPE, cwd = 'G24/' + str(self.fq))
But nothing came out
I tried:
Popen(comnd_input, stdout = PIPE, stderr = PIPE, cwd = 'G24/' + str(self.fq))
yet nothing either. Tried save it as a variable and do .communicate(), nothing.
So I don't know what wrong I did?
thanks,
Xp
refer to here.
It is because of the '>' redirecting. I modified the code to:
Popen(comnd, stdout = PIPE, stderr = PIPE, shell = True, cwd = 'G14/' + str(self.fq))
And it worked.

error in system command to output

I want to run ext software (hmmer) commands through perl in a loop for input files (in linux).
I used this line
system "hmmbuild $outfile $files";
where $outfile is my output file and $files in my input files. hmmbuild is the command for the ext software.
When I run the program it gives me error code for the output file GLOB(0x1b94b220).
Can any one help me where I am wrong and how can it be corrected?
I tried exec command also with back tick and brackets.
This is the exact output message i got. How can I print my result to output file ($outfile)?
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `hmmbuild --amino GLOB(0x11eb3220) aproNOG00001'
script goes here..
#!/usr/bin/perl
use Bio::AlignIO;
use Bio::Align::AlignI;
my $allfiles= 'allfilenames_alpha_hmms.txt';
system "module load hmmer/3.1b1";
print "loaded hmmer\n";
open(FIH, $allfiles);
while ($min=<FIH>)
{ chomp($min); my #pats=split " ",$min;
foreach my $files(#pats) {
print $files; print "\n";
open(my $outfile, '>',"$prefix.hmm");
system "hmmbuild --amino $outfile $files";
print $outfile;
print "file saved\n";
# }
}
}
print "\n\n\n\t ###\tDONE\t### \n\n";
how can i print my result to output file ($outfile)
I take it hmmbuild expects a path to a file? Pass the path to the file rather than what's in $outfile.
system "hmmbuild --amino $prefix.hmm $files";

Perl Index function not working?

I'm trying to test if a backtick output string (it is a string, right??) contains a substring.
my $failedCoutner = 0;
my $tarOutput = `tar -tvzf $tgzFile`;
print "$tarOutput\n";
my $subStr = "Cannot open: No such file or directory";
if (index($tarOutput, $subStr) != -1)
{
push(#failedFiles, $tgzFile);
$failedCounter++;
print "Number of Failed Files: $failedCounter\n\n\n";
}
print "Number of Failed Files: $failedCounter\n\n\n";
But this isn't working. It never enters the if statement.
The backtick output:
tar (child): /backup/Arcsight/EDSSIM004: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
Number of Failed Files: 0
Clearly the substring is in the first line. Why won't it recognize this??
tar, like most programs, writes error messages to STDERR. That's the purpose of STDERR.
Backticks only capture STDOUT.
You could redirect tar's STDERR to its STDOUT, but why not just check its exit code.
system('tar', '-tvzf', $tgzFile);
die "Can't launch tar: $!\n" if $? == -1;
die "tar killed by signal ".($? & 0x7F) if $? & 0x7F;
die "tar exited with error ".($? >> 8) if $? >> 8;
Advantages:
Catches all errors, not just one.
The output isn't held up until tar finishes before being sent to the screen.
It solves the problem of archives with shell metacharacters (e.g. spaces) in their name without invoking String::ShellQuote's shell_quote.
Check if backticks produced an error with $?:
use warnings;
use strict;
my $tarOutput = `tar -tvzf doesnt_exist.tar.gz`;
if ($?) {
print "ERROR ... ERROR ... ERROR\n";
}
else {
# do something else
}
__END__
tar (child): doesnt_exist.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
ERROR ... ERROR ... ERROR

Why is my Perl script that calls FTP all of a sudden failing?

I have a script that has been running for over a year and now it is failing:
It is creating a command file:
open ( FTPFILE, ">get_list");
print FTPFILE "dir *.txt"\n";
print FTPFILE "quit\n";
close FTPFILE;
Then I run the system command:
$command = "ftp ".$Server." < get_list | grep \"\^-\" >new_list";
$code = system($command);
The logic the checks:
if ($code == 0) {
do stuff
} else {
log error
}
It is logging an error. When I print the $code variable, I am getting 256.
I used this command to parse the $? variable:
$exit_value = $? >> 8;
$signal_num = $? & 127;
$dumped_core = $? & 128;
print "Exit: $exit_value Sig: $signal_num Core: $dumped_core\n";
Results:
Exit: 1 Sig: 0 Core: 0
Thanks for any help/insight.
Mel - you might gain a bit more information by looking at standard error output of the ftp command.
1) Does the FTP command work by hand from shell prompt?
2) If command line ftp works, capture the output (stdout and stderr) of the ftp command and print it in Perl script. For a couple of ways to do so, see perlfaq8 - How can I capture STDERR from an external command?
The two easiest apporaches are these:
my $output = `$command 2>&1`;
my $pid = open(PH, "$command 2>&1 |");
while (<PH>) { print "Next line from FTP output: $_"; }
3) As wisely noted by Snake Plissken in a comment, an alternate (and more idiomatic and possibly easier) approach is to scrap the system call to "ftp" command and instead use Net::FTP Perl module.