daily pastebin goal
24%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. stdout_output = open('testfile.txt','w')
  2. process = subprocess.Popen(command,stdout=stdout_output,shell=True)
  3. stdout_read = open('testfile.txt','r')
  4. data = stdout_read.read()
  5. print data
  6.    
  7. stdout_output = open('testfile.txt','w')
  8. process = subprocess.Popen(command,stdout=stdout_output,shell=True)
  9. time.sleep(3)
  10. stdout_read = open('testfile.txt','r')
  11. data = stdout_read.read()
  12. print data
  13.    
  14. data = subprocess.check_output(command, shell=True)
  15. print data
  16.    
  17. def do_stuff(proc, filename):
  18.     # proc.poll() check if proc has ended
  19.     while proc.poll() is None:
  20.         print('Here you do whatever you want while you wait for the process')
  21.         # do other stuff
  22.         ping_pong.play()
  23.     # here we are sure the command terminate and wrote his output
  24.     proc.stdout.close()
  25.     with open(filename) as f:
  26.         return f.read()
  27.  
  28. stdout_file = open('some_file', 'w')
  29. process = Popen(['command'], stdout=stdout_file)
  30. output = do_stuff(process, 'some_file')
  31.    
  32. def reader(fd, finished):
  33.     while not finished.is_set():
  34.         data = fd.read()
  35.         if data: print(data)
  36.         time.sleep(SOME_TIMEOUT)
  37.  
  38. process = subprocess.Popen(command,stdout=stdout_output,shell=True)
  39. finished = threading.Event()
  40. reader_thread = threading.Thread(target=reader, args=(stdout_output, finished))
  41. reader_thread.start()
  42. process.wait()
  43. finished.set()
  44. reader_thread.join()
  45.    
  46. stdout_output = open('testfile.txt','w', 0)
  47.    
  48. import subprocess
  49.  
  50. """
  51. notifier.sh
  52. ------------------
  53.  
  54. echo "This is me"
  55. sleep 4
  56. echo "This is me again"
  57.  
  58. ------------------
  59. """
  60.  
  61. command = ['bash', 'notifier.sh']
  62. process = subprocess.Popen(command, stdout=subprocess.PIPE)
  63.  
  64. while True:
  65.     data = process.stdout.readline()
  66.     print data
  67.     if 'This is me again' in data:
  68.         break
RAW Paste Data
Top