A few scripts related to subprocess-module of Python3
diff --git a/python3/os-module/f18 b/python3/os-module/f18
index ec81fed..3219c4b 100644
--- a/python3/os-module/f18
+++ b/python3/os-module/f18
@@ -4,14 +4,20 @@
>>> os.getgid()
1000
+-----------------------------------------------------------------------------------------------------------------
+
Returns user id of current process
>>> os.getuid()
1000
+-------------------------------------------------------------------------------------------------------------------
+
Returns process id of current process
>>> os.getpid()
2464
+-------------------------------------------------------------------------------------------------------------------
+
Returns information identifying current operating system
>>> os.uname()
posix.uname_result(sysname='Linux', nodename='localhost.localdomain', release='4.0.4-301.fc22.x86_64', version='#1 SMP Thu May 21 13:10:33 UTC 2015', machine='x86_64')
@@ -35,3 +41,12 @@
dir2 f11.py f13.py f15.py f17.py f2.py f4.py f6.py f8.py out.jpg README sample.tmp
f10.py f12.py f14.py f16.py f1.py f3 f5.py f7.py f9.py Python-Logo.jpg sample
+-----------------------------------------------------------------------------------------------------------------
+returns a list with the contents of directory defined by path
+
+>>> os.listdir("/home/dshikhar/Documents/my-code/python3/os-module")
+['f14.py', 'f13.py', 'f18', '.f18.swp', 'f17.py', 'sample', '.new.swp', 'f8.py', 'f6.py', 'f9.py', 'sample.tmp', 'Python-Logo.jpg', 'f15.py', 'out.jpg', 'f5.py', 'f1.py', 'README', 'f16.py', 'f7.py', 'f11.py', 'f10.py', 'dir1', 'f2.py', 'f4.py', 'f3', 'f12.py']
+
+-------------------------------------------------------------------------------------------------------------------
+
+
diff --git a/python3/subprocess-module/README b/python3/subprocess-module/README
new file mode 100644
index 0000000..c1bb2b1
--- /dev/null
+++ b/python3/subprocess-module/README
@@ -0,0 +1,7 @@
+Subprocess Module
+
+- used for dealing with external commands
+- it is used to spawn process, connect to their input/output/error pipes and obtain return codes
+- intended to be replacement of os.system, os.spawn, os.popen
+
+
diff --git a/python3/subprocess-module/abc b/python3/subprocess-module/abc
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/python3/subprocess-module/abc
diff --git a/python3/subprocess-module/f1 b/python3/subprocess-module/f1
new file mode 100644
index 0000000..33323e9
--- /dev/null
+++ b/python3/subprocess-module/f1
@@ -0,0 +1,63 @@
+Instead of using os.system, we can use subprocess.Popen. It helps to get the output of the script.
+Functions like check_output, check_call use Popen internally.
+
+>>> import subprocess
+>>> x = subprocess.Popen(['touch', 'xyz'])
+>>> x
+<subprocess.Popen object at 0x7f80b0d66208>
+>>> print (x)
+<subprocess.Popen object at 0x7f80b0d66208>
+>>> x.poll()
+0
+>>> x.returncode
+0
+
+[dshikhar@localhost subprocess-module]$ ls
+README xyz
+--------------------------------------------------------------------------------------------
+>>> p = subprocess.Popen(['cp', '-r', 'xyz', 'abc'])
+
+[dshikhar@localhost subprocess-module]$ ls
+abc README xyz
+----------------------------------------------------------------------------------------------
+To emulate the behaviour of os.system, optional parameter shell has to be set to true
+
+>>> q = subprocess.Popen("touch hello", shell = True)
+
+[dshikhar@localhost subprocess-module]$ ls
+abc hello README xyz
+
+--------------------------------------------------------------------------------------------
+To catch the output of a shell command into Python, we set the optional parameter stdout to subprocess.PIPE
+
+>>> process = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE)
+>>> print (process.stdout.read())
+b'total 4\n-rw-rw-r--. 1 dshikhar dshikhar 0 Jun 9 14:53 abc\n-rw-rw-r--. 1 dshikhar dshikhar 0 Jun 9 14:55 hello\n-rw-rw-r--. 1 dshikhar dshikhar 222 Jun 9 14:27 README\n-rw-rw-r--. 1 dshikhar dshikhar 0 Jun 9 14:51 xyz\n'
+
+-------------------------------------------------------------------------------------------
+
+
+>>> process = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE)
+>>> process.wait()
+0
+
+---------------------------------------------------------------------------------------------------------
+Run command with arguments and return its output as a byte string
+
+>>> ls_output = subprocess.check_output(['ls'])
+>>> ls_output
+b'abc\nf1\nhello\nREADME\nxyz\n'
+
+
+---------------------------------------------------------------------------------------------------------
+
+>>> subprocess.call('ls | wc -l', shell=True)
+5
+0
+>>> subprocess.call('ls', shell=True)
+abc f1 hello README xyz
+0
+
+---------------------------------------------------------------------------------------------------------
+
+
diff --git a/python3/subprocess-module/f2.py b/python3/subprocess-module/f2.py
new file mode 100755
index 0000000..6587cc2
--- /dev/null
+++ b/python3/subprocess-module/f2.py
@@ -0,0 +1,33 @@
+#Fix errors
+
+from subprocess import Popen, PIPE
+from threading import Thread
+from queue import Queue, Empty
+
+io_q = Queue()
+
+def stream_watcher(identifier, stream):
+ for line in stream:
+ io_q.put((identifier, line))
+ if not stream.closed():
+ stream.close()
+
+proc = Popen('/home/dshikhar/Documents/my-code/python3/subprocess-module/', stdout=PIPE, stderr=PIPE)
+
+thread(target=stream_watcher, name='stdout-watcher',
+ args=('STDOUT', proc.stdout)).start()
+Thread(target=stream_watcher, name='stderr-watcher',
+ args=('STDERR', proc.stderr)).start()
+
+def printer():
+ while True:
+ try:
+ item = io_q.get(True, 1)
+ except Empty:
+ if proc.poll() is not None:
+ break
+ else:
+ identifier, line = item
+ print (identifier + ':', line)
+
+Thread(target = printer, name = 'printer').start()
diff --git a/python3/subprocess-module/f3.py b/python3/subprocess-module/f3.py
new file mode 100755
index 0000000..f8eac7f
--- /dev/null
+++ b/python3/subprocess-module/f3.py
@@ -0,0 +1,10 @@
+#!/usr/bin/python3
+
+#Ping program using subprocess
+
+import subprocess
+
+host = input("Enter a host to ping:\t")
+p1 = subprocess.Popen(['ping', '-c 2', host], stdout=subprocess.PIPE)
+output = p1.communicate()[0]
+print (output)
diff --git a/python3/subprocess-module/hello b/python3/subprocess-module/hello
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/python3/subprocess-module/hello
diff --git a/python3/subprocess-module/xyz b/python3/subprocess-module/xyz
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/python3/subprocess-module/xyz