0

I have a problem and I would really appreciate any kind of help!I am using PyQt4 and Python.I have a batch file.I am trying to make a PyQt4 gui where I will press a button,the batch file will start running,somehow I will collect only some of the batch's output in a txt file , I will process these results and then they will be shown in the gui.I have two questions:

1)Is there any way to link the batch file with a button in PyQt so that I can launch it through the gui (by pressing the button)?

2)Is there anything that I can add into my PyQt code that will collect the batch's output into a txt file? Thank you!

KeatC
  • 27
  • 1
  • 7
  • Can you tell what is the batch file like? As in how do you execute it in general (command). And what is the output that you want to collect? As in is it shown on screen? – smitkpatel Feb 19 '15 at 05:10
  • When I double-click the batch file, a cmd appears where I answer a couple of yes/no questions and as soon as I answer all of them it starts running.When it stops running its results(6-7 files) appear in the same folder that the batch file is in,in a format that isn't txt.For the second question,if I didn't want to create a gui,I would simply open them and move them to a txt file.I know how to do that.But now that I have the gui,it seems wrong to write one by one all the files and open them inside the gui. – KeatC Feb 19 '15 at 12:31
  • @KeatC. Personally, I would put all my effort into eliminating the batch file altogether. Anything a batch file can do, python can do much better. The equivalent of the yes/no questions should be implemented as part of the gui, and the output should be displayed directly by the gui instead of writing it to disk first. Of course, the specifics of how to go about doing this depends on what the batch file actually does... – ekhumoro Feb 20 '15 at 18:34
  • @ekhumoro I know,you're right,but I am trying to avoid it because I am not so sure that I can manage to do it.I am quite a newbie to programming and I am trying to keep it simple, for now at least.Later,when I finish doing the gui,I may try doing it your way.Although I don't know much about batch files.I would really appreciate it if you could suggest me something to read about batch files that will help me later 'converting' a batch file to python... – KeatC Feb 22 '15 at 17:02
  • @KeatC. What is this batch file? Can you put it up on [pastebin](http://pastebin.com/) or something so we can see whether it's feasible to port it to python? – ekhumoro Feb 22 '15 at 17:08
  • @ekhumoro Thank you so much,but I really don't have the time to deal with it right now.Maybe later,when I have the time,I will come back,post it and learn more about batch files... – KeatC Mar 01 '15 at 21:33

1 Answers1

0

Use subprocess module to load and execute the batch

This thread might help you

Community
  • 1
  • 1
finmor
  • 451
  • 4
  • 7
  • this thread seems very helpful!!thank you!!May i ask something else?To connect the button,I must connect the button with the popen() fuction?like self.connect(buttonforbatch, SIGNAL("clicked()"),self.bfunction) ,where bfunction is def bfunction(self): p = Popen("batchfile.bat", cwd=r"c:\directory\containing\batchfile") ??or not?? – KeatC Feb 22 '15 at 16:54
  • Well, yes something like that. Possibly inside bfunction you might need to add more code than just Popen. (handling the output, errors etc) – finmor Feb 22 '15 at 17:14
  • Ok,thanks!!May I ask you one more thing if it's not trouble??In this thread,they use stdout,stderr=p.communicate(). When I run my batch file,I have multiple files as a result,but I use only some of them (sorry,I forgot to write that in my question) .In this stdout that I get with communicate() how can I chose and use only the files that I want?Because although the batch file runs normally I have no idea how to use this stdout.I even tried with open("boutput.txt","w") as f: f.write(stdout) to see what it's like,but I had an error TypeError: expected a character buffer object... – KeatC Mar 01 '15 at 21:50
  • With 'p = subprocess.Popen(filepath, stdout = subprocess.PIPE)' , 'stdout = p.communicate()' all the standard output created when the batch is ran will be stored in stdout. You''ll have there what would be displayed on the screen if you ran your batch from a terminal. But if you want data from the newly created files, you should read them, not stdout variable. – finmor Mar 02 '15 at 12:29