Passing arguments to Python function using QProcess
-
@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
@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 -
@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"; }
Application output:
QMessageBox appears without problems. -
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.
@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.")
@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.
-