I try to execute command in cmd from Python and save the output. I have the following code:
import subprocess
import matplotlib.pyplot as pyplot
MyOut = subprocess.Popen("pytr portfolio",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout,stderr = MyOut.communicate()
chart_data = []
days = []
t = 0
new_char = stdout.split()
print(new_char)
netValue = float(new_char[new_char.index(b'netValue') + 2])
file = open('netValue.txt','a')
file.write(str(netValue))
file.write('\n')
file.close()
file = open('netValue.txt','r')
for line in file:
t += 1
print(line)
chart_data.append(line)
days.append(t)
file.close()
pyplot.plot(days,chart_data)
pyplot.title('depot_performance')
pyplot.show()
On my pc it works well but when I run this on my laptop i get the following error message:
Traceback (most recent call last):
File "c:\Users\dinok\OneDrive\Desktop\Python_Scripts\depot_statistics.py", line 9, in <module>
MyOut = subprocess.Popen("pytr portfolio",
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 969, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1438, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden
I thought it is path related so that the module is not found but was not able to find a solution. What could cause this problem and how to fixt it?
Python's subprocess.Popen first parameter should be a list, the executable and the executable arguments. To fix it just do like the following snippet:
MyOut = subprocess.Popen(["pytr", "portfolio"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
Related
import sys
import subprocess
def generateFiles(numFiles):
for i in range(0,numFiles):
postfixDir = str(i)
subprocess.check_call(['mkdir','example' + postfixDir])
return
def main():
numFiles = int(sys.argv[1])
generateFiles(numFiles)
main()
after i enter commandline python .\batchingCommands.py 5
it showing the following erros:
PS E:\Projects\Python_Projects\Python_Automation\Scripts> python .\batchingCommands.py 5
Traceback (most recent call last):
File "E:\Projects\Python_Projects\Python_Automation\Scripts\batchingCommands.py", line 13, in <module>
main()
File "E:\Projects\Python_Projects\Python_Automation\Scripts\batchingCommands.py", line 11, in main
generateFiles(numFiles)
File "E:\Projects\Python_Projects\Python_Automation\Scripts\batchingCommands.py", line 6, in generateFiles
subprocess.check_call(['mkdir','example' + postfixDir])
File "E:\Development_tools\Python39\lib\subprocess.py", line 368, in check_call
retcode = call(*popenargs, **kwargs)
File "E:\Development_tools\Python39\lib\subprocess.py", line 349, in call
with Popen(*popenargs, **kwargs) as p:
File "E:\Development_tools\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "E:\Development_tools\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
i'm trying to create directory automatically
Python is telling you it asked the operating system to run the program mkdir, which doesn't exist. Unlike in unix, mkdir isn't an actual program on Windows - it's built into powershell and command.com themselves.
To instruct python to run the given command as arguments to a shell - instead of a raw program - use the shell kwarg.
>>> subprocess.check_call(['mkdir', 'foo'], shell=True)
0
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.
I wanted to create a Minecraft Launcher using Python today, but the game cannot be started.
My Code:
import minecraft_launcher_lib
import subprocess
minecraft_directory = "C:\\Users\\Mert KAPLANDAR\\AppData\\Roaming\\.minecraft"
options = minecraft_launcher_lib.utils.generate_test_options()
minecraft_command = minecraft_launcher_lib.command.get_minecraft_command("1.8.9", minecraft_directory, options)
subprocess.call(minecraft_command)
Error:
Traceback (most recent call last):
File "C:\Users\Mert KAPLANDAR\Desktop\launcher.py", line 10, in <module>
subprocess.call(str(minecraft_command))
File "C:\Users\Mert KAPLANDAR\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 349, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\Mert KAPLANDAR\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\Mert KAPLANDAR\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] Sistem belirtilen dosyayı bulamıyor
#Mert Kaplander, I would believe the error might be because you don't have that version of Minecraft installed. You can check with the following method:
minecraft_launcher_lib.utils.get_installed_versions(minecraft_dir)
example:
import subprocess
import minecraft_launcher_lib
from colorama import Fore, init
for version in minecraft_launcher_lib.utils.get_installed_versions(minecraft_dir):
print(Fore.GREEN+ f"{version}")
Other than that, your code is an exact copy from the documentation. So I don't see an other error.
I'm trying to delete the last line of the command line by calling os.system("cls") but that returns a weird symbol
I tried using sub-processing to delete the last line but doing that returns an Error
Traceback (most recent call last):
File "D:\GameDev-Programming\Python\puzzles\puzzles 1\main.py", line 11, in <module>
clear()
File "D:\GameDev-Programming\Python\puzzles\puzzles 1\main.py", line 6, in clear
x = call('clear' if os.name == 'posix' else 'cls')
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 349, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
The code that i copied from the internet
from subprocess import call
import os
def clear():
x = call('clear' if os.name == 'posix' else 'cls')
print("fa")
clear()
How do I fix these Issues? if I can't is there other ways to delete the last line of the Command-Line?
Edit:
Looks like the issue is from Pycharm(the ide im using)
I turned on the 'Emulate terminal in output console' in 'Run/Debug Configuration'
The error message ...
FileNotFoundError: [WinError 2] The system cannot find the file specified
... implies that the command you are trying to execute is not an external command. So it must be a command that is implemented by the shell. You need to add the shell=True argument:
def clear():
x = call('clear' if os.name == 'posix' else 'cls', shell=True)
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.