File not found error using python - python

I want to use MafftCommandline to align my data, but i get following error:
Traceback (most recent call last):
File "C:\Users\Rimvis\Desktop\asd\bioinformatika2_Working.py", line 35, in <mo
dule>
stdout, stderr = mafftCline() # Note that MAFFT will write the alignment to
stdout, which you may want to save to a file and then parse
File "C:\Python27\lib\site-packages\Bio\Application\__init__.py", line 475, in
__call__
shell=(sys.platform!="win32"))
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
my code is as following :
dataToProcess = "dataToProcess.fa"
file = open(dataToProcess, "w")
arrayOfSequences = []
for sequence in blast.alignments:
if sequence.length > blast.alignments[0].length * 80 / 100:
sequenceToAppend = SeqRecord(Seq(sequence.hsps[0].sbjct), id=sequence.title)
arrayOfSequences.append(sequenceToAppend)
SeqIO.write(arrayOfSequences, file, "fasta")
file.close()
maffPath = "..\mafft-win\mafft.bat"
mafftCline = MafftCommandline(maffPath, input=dataToProcess)
stdout, stderr = mafftCline() # Note that MAFFT will write the alignment to stdout, which you may want to save to a file and then parse
alignedData = "aligned.fa"
alignedFile = open(alignedData, "w")
alignedFile.write(stdout)
alignedFile.close()
aligned = AlignIO.read(alignedData, "fasta")
i was using this tutorial as an example

As #willOEM has said, the script is looking for a file in a relative directory.
Your script assumes that its file is located in the same directory as your "dataToProcess" fasta file.
If you have moved your script or are trying to open a file located elsewhere then it will raise this error.
You'll need to change your dataToProcess, maffPath and alignedFile to refer to the absolute path.

The problem was that i needed to escape slashes.
And use maffPath = "..\\mafft-win\\mafft.bat" instead of maffPath = "..\mafft-win\mafft.bat"

Related

FileNotFoundError: [WinError 2] The system cannot find the file specified using whisper

I've been trying to use OpenAI's whisper to transcribe some text.
Whenever I run, I get a FileNotFounderror.
My code is as follows:
import whisper
import os
print(os.listdir())
# f = open('test_text.txt', 'r')
# content = f. read()
# print(content)
# f. close()
audio = 'Users/geoff/Downloads/micro-machines.wav'
model = whisper.load_model("tiny")
result = model.transcribe('micro-machines.wav', fp16=False)
print(result['text'])
The commented out part with the opening of the text file was done as a test and runs each time without issue.
When the code prints the directory, I get the following correct list of files, But then the error follows immediarely after:
['.idea', 'main.py', 'micro-machines.wav', 'test_text.txt', 'venv']
Traceback (most recent call last):
File "C:\Users\geoff\PycharmProjects\pythonProject3\main.py", line 16, in <module>
result = model.transcribe('micro-machines.wav', fp16=False)
File "C:\Users\geoff\PycharmProjects\pythonProject3\venv\lib\site-packages\whisper\transcribe.py", line 82, in transcribe
mel = log_mel_spectrogram(audio)
File "C:\Users\geoff\PycharmProjects\pythonProject3\venv\lib\site-packages\whisper\audio.py", line 111, in log_mel_spectrogram
audio = load_audio(audio)
File "C:\Users\geoff\PycharmProjects\pythonProject3\venv\lib\site-packages\whisper\audio.py", line 42, in load_audio
ffmpeg.input(file, threads=0)
File "C:\Users\geoff\PycharmProjects\pythonProject3\venv\lib\site-packages\ffmpeg\_run.py", line 313, in run
process = run_async(
File "C:\Users\geoff\PycharmProjects\pythonProject3\venv\lib\site-packages\ffmpeg\_run.py", line 284, in run_async
return subprocess.Popen(
File "C:\Users\geoff\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\geoff\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
There seems to be a mismatch in the file paths you've used for the commented part as observed from the output:
C:\Users\geoff\ PycharmProjects \ pythonProject3
and the following part.
audio = 'Users/geoff/Downloads/micro-machines.wav'
You may verify where the .wav file exists - in 'Downloads' or in 'pythonProject3' folder and try again.

FileNotFoundError: [WinError 2] The system cannot find the file specified while trying to use pysndfx

I'm trying to sound process a wav file with python and pysndfx but getting this weird error. I've tried many different path formats and many different paths. Even thought os.path.isfile() returns true it still comes up with this error. Any help would be greatly appreciated.
from pysndfx import AudioEffectsChain
import os
in_file = os.getcwd() + "\\" + "a.mp3"
in_file = in_file.replace("\\", "//")#tried many things here, tried to it without any replacing
if os.path.isfile(in_file):
print("fileyes") #This returns true
else:
print("not a file")
print(in_file)
fs = 44100
fx = (AudioEffectsChain().
reverb().
delay().
phaser()
)
fx(in_file,"apro.mp3")
Here's the error
fileyes
E://PyEarTraning//Test//a.mp3
Traceback (most recent call last):
File "e:/PyEarTraning/Test/test.py", line 28, in <module>
fx(in_file,"E:\\PyEarTraning\\Test\\apro.mp3")
File "C:\Program Files (x86)\Python38-32\lib\site-packages\pysndfx\dsp.py", line 368, in __call__
infile = FilePathInput(src)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\pysndfx\sndfiles.py", line 29, in __init__
stdout, stderr = Popen(shlex.split(info_cmd, posix=False),
File "C:\Program Files (x86)\Python38-32\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files (x86)\Python38-32\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
I can't comment (yet) so i'll ask here. Do you have your file in the same directory (folder) as the python program? If not then it won't work even if the file does actually exist somewhere. Try to copy or move both your file and program code into a new/the same folder.

python check_output FileNotFoundError: [WinError 2] The system cannot find the file specified

I'm running the next piece of code to check the type of my file:
import subprocess as sub
output = sub.check_output(["file", "test.py"]).decode('ascii')
#output=sub.check_output(["file","C:/Users/Roger.That/PycharmProjects/test/test.py"]).decode('ascii')
and I keep getting the following error:
Traceback (most recent call last):
File "C:/Users/Roger That/PycharmProjects/test/test.py", line 2, in <module>
output = sub.check_output(["file", "test.py"]).decode('ascii')
File "C:\Users\Roger That\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 395, in check_output
**kwargs).stdout
File "C:\Users\Roger That\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 472, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\Roger That\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:\Users\Roger That\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
The full path of my test.py : "C:/Users/Roger.That/PycharmProjects/test/test.py"
output from gitbash :
$ file /c/Users/Roger\ That/PycharmProjects/test/test.py
/c/Users/Roger That/PycharmProjects/test/test.py: ASCII text, with CRLF line terminators
Any chance that it is related to the fact that I have a dot between "Roger" and "That"? Although it doesn't work also when I use the relative path (only the file name)
Update
I changed my user's dir name from Roger that to Roger.that, but it still didn't help:
python /c/Users/Roger.That/PycharmProjects/test/test.py
same error..
checked also :
import subprocess as sub
output = sub.check_output(["ls" "-l"])
got same error
I created from scratch the virtual environment and it solved my problem.

mkstemp opening too many files

I'm using subprocess.run in a loop (more than 10 000 times) to call some java command.
Like this:
import subprocess
import tempfile
for i in range(10000):
ret = subprocess.run(["ls"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(_, name) = tempfile.mkstemp()
with open(name, 'w+') as fp:
fp.write(ret.stdout.decode())
However, after some time, I got the following exception:
Traceback (most recent call last):
File "mwe.py", line 5, in <module>
ret = subprocess.run(["ls"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
File "/usr/lib/python3.5/subprocess.py", line 693, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1454, in _execute_child
errpipe_read, errpipe_write = os.pipe()
OSError: [Errno 24] Too many open files
Am I missing something to close some file descriptor?
Thanks
mkstemp returns an already open file descriptor fd followed by the filename. You are ignoring the file descriptor (your choice of the name _ suggests you have explicitly chosen to ignore it) and as a result you are neglecting to close it. Instead, you open the file a second time using the filename, creating a file object that contains a second file descriptor for the same file. Regardless of whether you close that second one, the first one remains open.
Here's a fix to the mkstemp approach:
temporaryFiles = []
for i in range(1000):
...
fd, name = tempfile.mkstemp()
os.write(fd, ... )
os.close(fd)
temporaryFiles.append(name) # remember the filename for future processing/deletion
Building on Wyrmwood's suggestion in the comments, an even better approach would be:
temporaryFiles = []
for i in range(1000):
...
with tempfile.NamedTemporaryFile(delete=False) as tmp:
# tmp is a context manager that will automatically close the file when you exit this clause
tmp.file.write( ... )
temporaryFiles.append(tmp.name) # remember the filename for future processing/deletion
Note that both mkstemp and the NamedTemporaryFile constructor have arguments that allow you to be more specific about the file's location (dir) and naming (prefix, suffix). If you want to keep the files, you should specify dir so that you keep them out of the default temporary directory, since the default location may get cleaned up by the OS.

Python. Send Uploaded File To Remote Server

In My Flask App, i want to upload a file to a remote server.
i tried this code but i get an error
import subprocess
import os
c_dir = os.path.dirname(os.path.abspath(__file__))
myfile = open(c_dir + '\\cape-kid.png')
p = subprocess.Popen(["scp", myfile, destination])
sts = os.waitpid(p.pid, 0)
this was just a test file. there's an image in the same directory as my test python file. the error said:
Traceback (most recent call last): File
"C:\Users\waite-ryan-m\Desktop\remote-saving\test-send.py", line 20,
in
p = subprocess.Popen(["scp", c_dir + '\cape-kid.png', 'destination']) File
"C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\subprocess.py",
line 711, in init
errread, errwrite) File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\subprocess.py",
line 959, in _execute_child
startupinfo) WindowsError: [Error 2] The system cannot find the file specified
With open() you open an file to read or write on it. What you want is to concatinate the string and use this as parameter for scp. Maybe the file you want to copy also doesn't exist - have you tried printing the path you constructed and checking it manually?
And have you defined destination anywhere? This message could also mean, that the system cannot find scp.

Categories

Resources