I'm trying to create a directory in a class constructor but there is a possibility of there is a directory with the same name as I want to create. So I'm trying the following approach.
class SubVideoExtractor:
def __init__(self, out_name=str()):
self.out_abs = os.getcwd() + "/output/" + out_name
try:
os.mkdir(self.out_abs)
except FileExistsError:
subprocess.Popen(f"rm -rf {self.out_abs}").wait()
os.mkdir(self.out_abs)
But when directory already exist, I got FileNotFound error during handling of FileExistsError.
How error looks like:
Traceback (most recent call last):
File "/home/lapestand/DEV/projects/SubVideoExtractor/SubVideoExtractor.py", line 22, in __init__
os.mkdir(self.out_abs)
FileExistsError: [Errno 17] File exists: '/home/lapestand/DEV/projects/SubVideoExtractor/output/16035609841603553594'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 95, in <module>
extractor = SubVideoExtractor(length=VIDEO_LENGTH, _format=VIDEO_FORMAT,
File "/home/lapestand/DEV/projects/SubVideoExtractor/SubVideoExtractor.py", line 24, in __init__
subprocess.Popen(f"rm -rf {self.out_abs}").wait()
File "/usr/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'rm -rf /home/lapestand/DEV/projects/SubVideoExtractor/output/16035609841603553594'
Why this is happening and how to solve?
You may want to continue even if the folder exists:
os.makedirs(path, exist_ok=True)
But you may prefer to really delete the folder before recreate it:
shutil.rmtree(path)
There are other ways to go about what you're trying to do without catching an exception. That said, after glancing at the docs, it appears you should be passing in an array with your command to Popen as follows:
subprocess.Popen(["rm", "-rf", f"{self.out_abs}"]).wait()
Related
I tried to use python to call the “ansys-fluent-core” package, but after I ran it, the following error occurred. I don't know where the problem is. Can you help me and give me some suggestions.
my code:
import ansys.fluent.core as pyfluent
session = pyfluent.launch_fluent(precision="double", processor_count=4, show_gui=True,
mode="solver")
error:
E:\HB-software\Anaconda\anaconda\envs\py38\python.exe C:\Users\86139\PycharmProjects\pythonProject2\main.py
Traceback (most recent call last):
File "C:\Users\86139\PycharmProjects\pythonProject2\main.py", line 2, in <module>
session = pyfluent.launch_fluent(precision="double", processor_count=4, show_gui=True,mode="solver")
File "E:\HB-software\Anaconda\anaconda\envs\py38\lib\site-
packages\ansys\fluent\core\launcher\launcher.py", line 560, in launch_fluent
subprocess.Popen(launch_string, **kwargs)
File "E:\HB-software\Anaconda\anaconda\envs\py38\lib\subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "E:\HB-software\Anaconda\anaconda\envs\py38\lib\subprocess.py", line 1311, in
_execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the specified file.
Process finished with exit code 1
I am trying to build a simple dataset for speech recognition using aneas, following the instruction here. However I'm stuck at the forced alignment, here is my code
from aeneas.executetask import ExecuteTask
from aeneas.task import Task
import os
import numpy
config_string = "task_language=eng | is_text_type=plain | os_task_file_format=json"
task = Task (config_string=config_string)
task.audio_file_path_absolute = "D:/documents/AI/open_door.mp3"
task.text_file_path_absolute = "D:/documents/AI/open_door.txt"
task.sync_map_file_path_absolute = "D:/documents/AI/syncmap.json"
ExecuteTask(task).execute()
task.output_sync_map_file()
Which gives the following error:
Traceback (most recent call last):
File "C:\Users\ACER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aeneas\ffprobewrapper.py", line 222, in read_properties
proc = subprocess.Popen(
File "C:\Users\ACER\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\ACER\AppData\Local\Programs\Python\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
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\ACER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aeneas\audiofile.py", line 357, in read_properties
properties = FFPROBEWrapper(
File "C:\Users\ACER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aeneas\ffprobewrapper.py", line 233, in read_properties
self.log_exc(u"Unable to call the '%s' ffprobe executable" % (self.rconf[RuntimeConfiguration.FFPROBE_PATH]), exc, True, FFPROBEPathError)
File "C:\Users\ACER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aeneas\logger.py", line 351, in log_exc
raise raise_type(raise_message)
aeneas.ffprobewrapper.FFPROBEPathError: Unable to call the 'ffprobe' ffprobe executable : [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\documents\AI\forced_alignment.py", line 9, in <module>
task.audio_file_path_absolute = "D:/documents/AI/open_door.mp3"
File "C:\Users\ACER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aeneas\task.py", line 120, in audio_file_path_absolute
self._populate_audio_file()
File "C:\Users\ACER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aeneas\task.py", line 241, in _populate_audio_file
self.audio_file.read_properties()
File "C:\Users\ACER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aeneas\audiofile.py", line 363, in read_properties
self.log_exc(u"Unable to call ffprobe executable", None, True, AudioFileProbeError)
File "C:\Users\ACER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aeneas\logger.py", line 351, in log_exc
raise raise_type(raise_message)
aeneas.audiofile.AudioFileProbeError: Unable to call ffprobe executable
[Finished in 0.4s]
Intuitively, I think it is because of the file paths, but changing it doesn't seem to work. I have never worked with python before so show me where I did wrong. Thank you.
I'm pretty sure it's because ffprobe isn't in your system's environmental variables.
Have you tried running python -m aeneas.diagnostics and is ffprobe in your path? Look into adding programs to your Path/system environment variables.
I am trying to create a GUI for working with the pip command with a text editor. Currently I am receiving an [Error 18]: Invalid cross-device link which traces back to os.rename which my program does not explicitly call.
I believe that this error occurs because pip is trying to call on a temporary file located on the /tmp/ file system and the actual folders are on the /usr/ file system (same computer different mount points). I have tried to resolve this by creating a new temporary directory in my current working directory via tempfile.TemporaryDirectory(dir='./') and feeding that into my Popen(..., cwd=NewTempDir). I have also tried setting the os.environ['TMPDIR'] = NewTempDir and both of these still lead to the Error 18.
self.tmp = tempfile.TemporaryDirectory(dir='./')
os.environ['TMPDIR'] = self.tmp
....
self.sp = subprocess.Popen([sys.executable, '-m', 'pip', \
pip_dict['process'], pip_dict['package']], \
stdout=subprocess.PIPE, stdin=subprocess.PIPE, \
cwd=self.tmp.name)
....
while True:
stdout = self.sp.stdout.read(1)
self.stdout += stdout
print(self.stdout)
if b'Proceed (y/n)?' in self.stdout:
try:
print('attempting response')
self.sp.stdin.write(b'y\n')
self.sp.stdin.flush()
break
except:
print('stdin write failed')
I expect the output to be the uninstalling of the named package.
My current output is:
Exception:
Traceback (most recent call last):
File "/usr/lib/python3.7/shutil.py", line 563, in move
os.rename(src, real_dst)
OSError: [Errno 18] Invalid cross-device link:
'/usr/lib/python3.7/site-packages/anytree' -> '/tmp/pip-
uninstall-ye0w908_'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.7/site-\
packages/pip/_internal/cli/base_command.py", line 179, in
main
status = self.run(options, args)
File "/usr/lib/python3.7/site-
packages/pip/_internal/commands/uninstall.py", line 75, in run
auto_confirm=options.yes, verbose=self.verbosity > 0,
File "/usr/lib/python3.7/site-
packages/pip/_internal/req/req_install.py", line 817, in uninstall
uninstalled_pathset.remove(auto_confirm, verbose)
File "/usr/lib/python3.7/site-
packages/pip/_internal/req/req_uninstall.py", line 360, in remove
moved.stash(path)
File "/usr/lib/python3.7/site-
packages/pip/_internal/req/req_uninstall.py", line 257, in stash
renames(path, new_path)
File "/usr/lib/python3.7/site-packages/pip/_internal/utils/misc.py",
line 303, in renames
shutil.move(old, new)
File "/usr/lib/python3.7/shutil.py", line 575, in move
rmtree(src)
File "/usr/lib/python3.7/shutil.py", line 491, in rmtree
_rmtree_safe_fd(fd, path, onerror)
File "/usr/lib/python3.7/shutil.py", line 449, in _rmtree_safe_fd
onerror(os.unlink, fullname, sys.exc_info())
File "/usr/lib/python3.7/shutil.py", line 447, in _rmtree_safe_fd
os.unlink(entry.name, dir_fd=topfd)
PermissionError: [Errno 13] Permission denied: 'search.py'
I do receive print("attempting response") before the code breaks.
Here is the link to the full program file... https://github.com/rscales02/porcupine/blob/pipgui/porcupine/plugins/pip_plugin.py
Thanks in advance!
I would like to execute an external compiled program in Python. In shell,
./bin/myProgram ./dir1/arg1 ./dir/arg2 arg3 arg3 arg4
And my Python script looks like:
import subprocess
subprocess.call(["./Users/Solar/Desktop/parent/child1/child2/bin/myProgram",
"/Users/Solar/Desktop/parent/child1/child2/dir1/arg1",
"/Users/Solar/Desktop/parent/child1/child2/dir1/arg2",
"arg3", "arg4", "arg5"])
But I get this error:
Traceback (most recent call last):
File "/Users/Solar/Desktop/test.py", line 5, in <module>
"32", "0.06", "15"])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Process finished with exit code 1
Could you help me? Thank you in advance!!
It's solved, I just had to execute
subprocess.call(["/Users/Solar/Desktop/parent/child1/child2/bin/myProgram",
"/Users/Solar/Desktop/parent/child1/child2/dir1/arg1",
...
Instead of
subprocess.call(["./Users/Solar/Desktop/parent/child1/child2/bin/myProgram",
"/Users/Solar/Desktop/parent/child1/child2/dir1/arg1",
...
Thank you people!!
when subprocess.Popen issues
OSError: [Errno 2] No such file or directory
that's only because the executable cannot be found (if one of the file-arguments was not found it's the sub-process which reports the error, and no exception is thrown)
In your case ./Users/... is not likely to be valid considering that /Users/ is valid (root against current dir) so that's your problem.
"./Users/Solar/Desktop/parent/child1/child2/bin/myProgram" should be "/Users/Solar/Desktop/parent/child1/child2/bin/myProgram"
Note: better coding could have avoided the error:
import subprocess
root = "/Users/Solar/Desktop/parent/child1/child2"
subprocess.call([os.path.join(root,"bin/myProgram"),
os.path.join(root,"dir1/arg1"),
os.path.join(root,"dir1/arg2"),
"arg3", "arg4", "arg5"])
I am trying to run a bash script in my python script, I'm sure there is a way to do what my bash script is doing in python instead but I am more focused on getting this working.
Here is the code I am running as root followed by the error:
import subprocess
#variable = subprocess.check_output('dmidecode', shell=True)
#print(variable)
#run program once as root then cron it as root
try :
file = open("/var/log/serialcontrol/dmidecode.txt", "r")
except FileNotFoundError:
file = open('/var/tmp/serialcontrol.bash', 'w')
file.write("#!/bin/bash/\nif [ ! -d /var/log/serialcontrol/]\nthen\n\tmkdir /var/log/serialcontrol/\nfi");
file.close()
subprocess.call("/var/tmp/serialcontrol.bash")
Heres the error
Traceback (most recent call last):
File "/home/trisimix/serialcontrol/serialcontrol.py", line 6, in <module>
file = open("/var/log/serialcontrol/dmidecode.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: '/var/log/serialcontrol/dmidecode.txt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/trisimix/serialcontrol/serialcontrol.py", line 11, in <module>
subprocess.call("/var/tmp/serialcontrol.bash")
File "/usr/lib/python3.5/subprocess.py", line 557, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
PermissionError: [Errno 13] Permission denied
The /var/tmp/serialcontrol.bash file that you create while handling the FileNotFoundError is not executable. Make it executable first before trying to execute it with subprocess.
import subprocess
import os
import stat
#variable = subprocess.check_output('dmidecode', shell=True)
#print(variable)
#run program once as root then cron it as root
try :
file = open("/var/log/serialcontrol/dmidecode.txt", "r")
except FileNotFoundError:
script = '/var/tmp/serialcontrol.bash'
with open(script, 'w') as file:
file.write("#!/usr/bin/env bash/\nif [ ! -d /var/log/serialcontrol/]\nthen\n\tmkdir /var/log/serialcontrol/\nfi");
st = os.stat(script)
os.chmod(script, st.st_mode | stat.S_IEXEC)
subprocess.call(script)
As #anishsane pointed out in a comment, another alternative to chmod-ing the script is to call it like this, and just drop the chmod:
subprocess.call(["/bin/bash", script])
Actually, if we can assume that bash is on PATH, then this would be more portable:
subprocess.call(["bash", script])