Using subprocess to show a command window and get the text from the window after the command has ran

 ̄綄美尐妖づ 提交于 2020-03-05 05:01:14

问题


I'm trying to have a command window open and then once the command has been executed to take the output of it into a variable. The issue that I'm having is that when I use the below command I get an empty string: self.srcEntry.get() and self.dstEntry.get() are the sources and destination folders for robocopy.

output = Popen(["start", "cmd", "/K", "RoboCopy.exe", f"{self.srcEntry.get()}", f"{self.dstEntry.get()}", "*.*", "/E", "/Z", "/MT:8"], stdout=PIPE, stdin=PIPE, shell=True)
print(output.communicate()[0].decode('utf-8'))

Also the program pauses until the command window has been closed (I don't have an issue with that). However when I use:

output = Popen(["RoboCopy.exe", f"{self.srcEntry.get()}", f"{self.dstEntry.get()}", "*.*", "/E", "/Z", "/MT:8"], stdout=PIPE, stdin=PIPE, shell=True)
print(output.communicate()[0].decode('utf-8'))

I can get the output but not the command window. This is an issue as if I have a big file to transfer over then I won't know how far it's gotten until it has actually finished.

来源:https://stackoverflow.com/questions/60208196/using-subprocess-to-show-a-command-window-and-get-the-text-from-the-window-after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!