Sox in subprocess not creating output file - python

I am writing a Python script to mix multiple audio files into one combined file. I am using the Sox command within a subprocess call; however, it is not creating a mixed output file. Does anyone know how to fix this?
subprocess.call('sox -m %s %s' % (' '.join(audios), audio_file.mp3), shell=True)
audios is a list of the mp3 files (written as a full path) that I have (e.g. "./Files/Music/a.mp3"), and audio_file.mp3 is the mixed audio file I wish to create. I have included the line !pip install sox at the top of the script.

Related

Execute python program with multiple files - Python - Bash

I have hundreds of XML files and I would like to parse it into CSV files. I already code this program.
To execute the python program I use this command (on VScode MS):
python ConvertXMLtoCSV.py -i Alarm120.xml -o Alarm120.csv
My question is, how change this script to integrate a sort of for loop to execute this program for each xml files ?
UPDATE
If my files and folders are organized like in the picture:
I tried this and execute the file .bat in windows10 but it does nothing:
#!/bin/bash
for xml_file in XML_Files/*.xml
do
csv_file=${xml_file/.xml/.csv}
python ConvertXMLtoCSV.py -i XML_Files/$xml_file -o CSV_Files/$csv_file
done
Ideally the for loop would be included inside your ConvertXMLtoCSV.py itself. You can use this to find all xml files in a given directory:
for file in os.listdir(directory_path):
if file.endswith(".xml"):
# And here you can do your conversion
You could change the arguments given to the script to be the path of the directory the xml files are located in and the path for an output folder for the .csv files. For renaming, you can leave the files with the same name but give the .csv extension. i.e.
csv_name = file.replace(".xml", ".csv")
If you want to keep your Python script as-is (process one file), and add the looping externally in bash, you could do:
#!/bin/bash
for xml_file in *.xml
do
csv_file=${xml_file/.xml/.csv}
python ConvertXMLtoCSV.py -i $xml_file -o $csv_file
done
After discussion, it appears that you wish to use an external script so as to leave the original ConvertXMLtoCSV.py script unmodified (as required by other projects), but that although you tagged bash in the question, it turned out that you were not in fact able to use bash to invoke python when you tried it in your setup.
This being the case, it is possible to adapt Rolv Apneseth's answer so that you do the looping in Python, but inside a separate script (let's suppose that this is called convert_all.py), which then runs the unmodified ConvertXMLtoCSV.py as an external process. This way, the ConvertXMLtoCSV.py will still be set up to process only one file each time it is run.
To call an external process, you could either use os.system or subprocess.Popen, so here are two options.
Using os.system:
import os
import sys
directory_path = sys.argv[1]
for file in os.listdir(directory_path):
if file.endswith(".xml"):
csv_name = file.replace(".xml", ".csv")
os.system(f'python ConvertXMLtoCSV.py -i {file} -o {csv_name}')
note: for versions of python too old to support f-strings, that last line could be changed to
os.system('python ConvertXMLtoCSV.py -i {} -o {}'.format(file,csv_name))
Using subprocess.Popen:
import subprocess
import sys
directory_path = sys.argv[1]
for file in os.listdir(directory_path):
if file.endswith(".xml"):
csv_name = file.replace(".xml", ".csv")
p = subprocess.Popen(['python', 'ConvertXMLtoCSV.py',
'-i', file,
'-o', csv_name])
p.wait()
You could then run it using some command such as:
python convert_all.py C:/Users/myuser/Desktop/myfolder
or whatever the folder is where you have the XML files.

The Generated File By The Command Line Doesn't Exist

I'm using SOX Command in Windows to Convert .wav files to a spectogram .png file, from the cmd directly it's working but in the python script it returns 1 but the image doesn't exist in the directory specified.
rootdir =r'C:\Users\Heba\output.wav'
wave_path = rootdir
wave_image_path = wave_path.replace(".wav", ".png").
cmdstring = 'sox "{}" -n spectrogram -r -o "{}"'.format(wave_path, wave_image_path)
subprocess.call(cmdstring, shell=True)
it doesn't provide any errors too, any help please?

How to place output of python script compiling latex document at desired location using "execute_process" command of cmake?

I have a cmake file from which i am executing a python script using "execute_command" as follows:
execute_process (COMMAND C:/Programs/Python27/python.exe "C:/packaging/doc/release_doc.py"
--var_ProjectName "${TARGET}"
--var_version "${_VERSION}" OUTPUT_FILE "C:/packaging/doc/")
When the cmake is executed i am not getting the output at the location specified inside "OUTPUT_FILE". The python file which i am executing is actually compiling a latex .tex file and hence generating a the corresponding pdf document along with the .log file, .aux file and .out file.
When i execute the python script from the location where the latex .tex document is located i get all the files generated at the same location and pdf is all alligned but when i have to execute the same python script from the cmake i am getting all the four files placed at the location different from .tex document and the pdf file alignment gets distrupted.
So, please suggest how can i have the this python file executed from within the cmake and the output of files at the location as i desire.
Is "C:/packaging/doc/" a folder? Then please replace it by something like "C:/packaging/doc/output.txt".
you can also try using OUTPUT_VARIABLE and check if it makes any difference:
execute_process (COMMAND COMMAND C:/Programs/Python27/python.exe "C:/packaging/doc/release_doc.py" --var_ProjectName "${TARGET}" --var_version "${_VERSION}" OUTPUT_VARIABLE test)
message(${test})
file(WRITE "C:/packaging/doc/output2.txt" "${test}")

Passing parameters to .exe file within cmd

So I have an .exe tool that needs to be executed in cmd that's used to convert some GIS data from one format to BAG format. The syntax looks like so:
C:\Caris\BDB\4.0\bin > carisbatch -r SurfacetoBAG [options] "input_file" "output_file"
I have a directory of about 40 files of GIS data, so I'd like to know how to automate the above cmd tool such that it will run through use all 40 files as "input_file". I've looked into using the subprocess() module wrapped in a for loop with python, but I am under the impression it can only be used for unix systems. Any ideas?
If you import os, you should be able to use
os.system('your command')
Regardless of platform (of course, the command string will vary between platforms)
you can use subprocess no problem on windows ...
example to follow
import subprocess
for file in my_files:
subprocess.Popen(["C:/Caris/BDB/4.0/bin/carisbatch.exe",'-r','SurfacetoBAGfile',file+".output"])
if you need to do it without the list format
for file in my_files:
subprocess.Popen('C:/Caris/BDB/4.0/bin/carisbatch.exe -r SurfacetoBAGfile "{0}" "{0}.output"'.format(file),shell=True)

Use curl to download multiple files

I have to use cURL on Windows using python script. My goal is: using python script get all files from remote directory ... preferably into local directory. After that I will compare each file with the files stored locally. I am able to get one file at a time but I need to get all of the files from remote directory.
Could someone please advice how to get multiple files?
I use this command:
curl.exe -o file1.txt sftp:///dir1/file1.txt -k -u user:password
thanks
I haven't tested this, but I think you could just try launching each shell command as a separate process to run them simultaneously. Obviously, this might be a bad idea if you have a large set of files, so you might need to manage that more carefully. Here's some untested code, and you'd need to edit the 'cmd' variable in the get_file function, of course.
from multiprocessing import Process
import subprocess
def get_file(filename):
cmd = '''curl.exe -o {} sftp:///dir1/{} -k -u user:password'''.format(filename, filename)
subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT) # run the shell command
files = ['file1.txt', 'file2.txt', 'file3.txt']
for filename in files:
p = Process(target=get_file, args=(filename,)) # create a process which passes filename to get_file()
p.start()

Categories