Passing arguments to Python function using QProcess
-
wrote on 16 Sept 2020, 11:49 last edited by giusirux
Hi everyone
My goal is to pass the path of the file to the xml_writer_path function whose signature is xml_writer_path(pathFile) in storage_retrieval.py
My code is:QString pathFile = QFileDialog::getSaveFileName(new QWidget, "Save file", "C:", "XML (*.xml);"); QProcess p; QStringList arguments { "-c", "from storage_retrieval import xml_writer_path, sys; print(xml_writer_path(str(sys.argv[1])))", pathFile}; p.start("python", arguments); p.waitForFinished();
This code doesn't work. Any suggestions?
-
Hi,
What exactly does not work ?
You should check if your process ends in error and if so what that error is.
You should also checkout the standard output and error channels for anything printed there. -
Hi everyone
My goal is to pass the path of the file to the xml_writer_path function whose signature is xml_writer_path(pathFile) in storage_retrieval.py
My code is:QString pathFile = QFileDialog::getSaveFileName(new QWidget, "Save file", "C:", "XML (*.xml);"); QProcess p; QStringList arguments { "-c", "from storage_retrieval import xml_writer_path, sys; print(xml_writer_path(str(sys.argv[1])))", pathFile}; p.start("python", arguments); p.waitForFinished();
This code doesn't work. Any suggestions?
wrote on 16 Sept 2020, 13:05 last edited by JonB@giusirux
Please act on what @SGaist has advised you.Try your command from outside of Qt at a terminal: I am not convinced the
import
is correct.Also you have chosen to pass
pathFile
using Qt/
syntax. Are you on Linux/MacOS? If you are on Windows, when testing outside Qt please pass the path with/
s to see whether that is acceptable to yourxml_writer_path()
method? -
Hi,
What exactly does not work ?
You should check if your process ends in error and if so what that error is.
You should also checkout the standard output and error channels for anything printed there.wrote on 16 Sept 2020, 13:18 last edited by@SGaist @JonB Hi
with xml_writer_path function, I write pathFile in a file which is located in my Qt project.
The strange thing is that if I execute the command "python -c "from storage_retrieval import xml_writer_path, sys; print(xml_writer_path(str(sys.argv[1]))) C:/Users/Giusi/Desktop/test.xml " from cmd, the function is executed correctly. -
@SGaist @JonB Hi
with xml_writer_path function, I write pathFile in a file which is located in my Qt project.
The strange thing is that if I execute the command "python -c "from storage_retrieval import xml_writer_path, sys; print(xml_writer_path(str(sys.argv[1]))) C:/Users/Giusi/Desktop/test.xml " from cmd, the function is executed correctly.wrote on 16 Sept 2020, 13:52 last edited by JonB@giusirux
Then you must act on @SGaist 's:You should check if your process ends in error and if so what that error is.
You should also checkout the standard output and error channels for anything printed there.
Look at
QProcess
documentation/examples for these.I must also pick you up on the exactitudes:
execute the command "python -c "from storage_retrieval import xml_writer_path, sys; print(xml_writer_path(str(sys.argv[1]))) C:/Users/Giusi/Desktop/test.xml " from cmd
The correct will actually be:
python -c "from storage_retrieval import xml_writer_path, sys; print(xml_writer_path(str(sys.argv[1])))" C:/Users/Giusi/Desktop/test.xml
Note the position of the
"
s. -
wrote on 16 Sept 2020, 16:01 last edited by
@JonB hi
So, that's it
I don't know why there is "None"P.S. : when in the last answer I used the double quotes before "python" and after " C:/Users/Giusi/Desktop/test.xml", it was to mention the command I ran in cmd. I'm sorry I wasn't clear
-
@JonB hi
So, that's it
I don't know why there is "None"P.S. : when in the last answer I used the double quotes before "python" and after " C:/Users/Giusi/Desktop/test.xml", it was to mention the command I ran in cmd. I'm sorry I wasn't clear
wrote on 16 Sept 2020, 17:20 last edited by JonB@giusirux
That looks good. TheNone
is doubtless being printed somewhere or a return result, I wouldn't worry about it. [EDIT: Oh I see, it's the output from yourprint(...)
.]Now we must take you back to: you still have not explained how you know "This code doesn't work"?
It is time you put in the code to show what is in stdard output/error when you run your
QProcess
. -
@giusirux said in Passing arguments to Python function using QProcess:
xml_writer_path
Does that method return anything to be printed ?
-
Then seeing None printed is normal since that method returns None.
-
@giusirux
That looks good. TheNone
is doubtless being printed somewhere or a return result, I wouldn't worry about it. [EDIT: Oh I see, it's the output from yourprint(...)
.]Now we must take you back to: you still have not explained how you know "This code doesn't work"?
It is time you put in the code to show what is in stdard output/error when you run your
QProcess
. -
@JonB when I run my QProcess, nothing is happening.
xml_writer_path 's goal is to write pathFile in a file which is included in my Qt project.
In fact, when I run my QProcess, the file is not modified -
wrote on 16 Sept 2020, 19:45 last edited by giusirux
@JonB
QObject::connect(buttonSave, &QPushButton::clicked, widget, saveFile);in saveFile:
void saveFile() { //PYTHON qDebug() << "save File"; QProcess p; QString filename = QFileDialog::getSaveFileName(new QWidget, "Save file", "C:", "XML (*.xml);"); qDebug() << filename; QStringList arguments { "-c", "from storage_retrieval import xml_writer_path, sys; print(xml_writer_path(str(sys.argv[1])))", filename }; p.start("python", arguments); p.waitForFinished(); QMessageBox::information(widget," ","done"); qDebug() << "end"; }
-
As already written, and quoted: read the standard output and standard error of your QProcess.
-
As already written, and quoted: read the standard output and standard error of your QProcess.
wrote on 16 Sept 2020, 20:05 last edited by giusirux@SGaist yes, sorry.. So:
void saveFile() { //PYTHON qDebug() << "save File"; QProcess p; QString filename = QFileDialog::getSaveFileName(new QWidget, "Save file", "C:", "XML (*.xml);"); qDebug() << filename; QStringList arguments { "-c", "from storage_retrieval import xml_writer_path, sys; print(xml_writer_path(str(sys.argv[1])))", filename }; p.start("python", arguments); p.waitForFinished(); qDebug() << p.readAllStandardOutput(); qDebug() << "-----"; qDebug() << p.readAllStandardError(); QMessageBox::information(widget," ","done"); qDebug() << "end"; }
in xml_writer_path:
def xml_writer_path(filepath): try: print("[+] Xml writer path in esecuzione...") flag=0 with open("metadati_segmentazione.txt", "r") as input_file, open("metadati_segmentazione2.txt", "w") as output_file: for line in input_file: if line.startswith("#metadati_fase3"): flag=1 output_file.write(line) elif line.startswith("s3name") and flag == 1: output_file.write(line.replace("s3name:","s3name:"+str(filepath))) else: output_file.write(line) input_file.close() output_file.close() os.remove("metadati_segmentazione.txt") os.rename("metadati_segmentazione2.txt","metadati_segmentazione.txt") print("[+] Esecuzione xml writer path terminata.") except: print("[-] Error xml writer path.")
-
@SGaist yes, sorry.. So:
void saveFile() { //PYTHON qDebug() << "save File"; QProcess p; QString filename = QFileDialog::getSaveFileName(new QWidget, "Save file", "C:", "XML (*.xml);"); qDebug() << filename; QStringList arguments { "-c", "from storage_retrieval import xml_writer_path, sys; print(xml_writer_path(str(sys.argv[1])))", filename }; p.start("python", arguments); p.waitForFinished(); qDebug() << p.readAllStandardOutput(); qDebug() << "-----"; qDebug() << p.readAllStandardError(); QMessageBox::information(widget," ","done"); qDebug() << "end"; }
in xml_writer_path:
def xml_writer_path(filepath): try: print("[+] Xml writer path in esecuzione...") flag=0 with open("metadati_segmentazione.txt", "r") as input_file, open("metadati_segmentazione2.txt", "w") as output_file: for line in input_file: if line.startswith("#metadati_fase3"): flag=1 output_file.write(line) elif line.startswith("s3name") and flag == 1: output_file.write(line.replace("s3name:","s3name:"+str(filepath))) else: output_file.write(line) input_file.close() output_file.close() os.remove("metadati_segmentazione.txt") os.rename("metadati_segmentazione2.txt","metadati_segmentazione.txt") print("[+] Esecuzione xml writer path terminata.") except: print("[-] Error xml writer path.")
wrote on 16 Sept 2020, 20:26 last edited by JonB@giusirux
This isn't what we meant, but never mind....Change your
try
to haveprint(filepath)
as its first statement.Change your
except
to print full information about the exception. Never just put anexcept
like you have with no detail into your code. You might even be better commenting out thetry
and theexcept
lines while you debug your issue. Put further print statements into the body to see where the error is occurring. All this is what is expected of debugging a program problem. -
@giusirux said in Passing arguments to Python function using QProcess:
metadati_segmentazione.txt
Where is that file exactly located ?
Do not use bare except, at least print the exception you got to know what is happening.
-
@giusirux said in Passing arguments to Python function using QProcess:
metadati_segmentazione.txt
Where is that file exactly located ?
Do not use bare except, at least print the exception you got to know what is happening.
wrote on 16 Sept 2020, 20:38 last edited by -
1/19