Python Forum
using subpocess for both reading and writing simultaneously
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using subpocess for both reading and writing simultaneously
#10
(May-30-2018, 08:11 PM)volcano63 Wrote: Command string as parameter may only be provided when it is coupled with shell=True; with default value False list is required (as you have shown)

I try not to use the shell=True mode... some bad experiences with the shell expansions.
I have done some additional tests and you are right that when you want to pass parameters with shell=False you need to pass them in a list. Is logical as Popen will try to blindly search for a file with the full name:
>>>with subprocess.Popen(['ls'], stdout=subprocess.PIPE) as p:
>>>    print(''.join(b.decode() for b in p.stdout.readlines()))
cpt.py
subp.py
>>> with subprocess.Popen('ls', stdout=subprocess.PIPE) as p:
>>>    print(''.join(b.decode() for b in p.stdout.readlines()))
cpt.py
subp.py
>>> with subprocess.Popen(['ls', '-r'], stdout=subprocess.PIPE) as p:
>>>    print(''.join(b.decode() for b in p.stdout.readlines()))
subp.py
cpt.py
>>> with subprocess.Popen('ls -r', stdout=subprocess.PIPE) as p:
>>>    print(''.join(b.decode() for b in p.stdout.readlines()))
Traceback (most recent call last):
  File "subp.py", line 27, in <module>
    with subprocess.Popen('ls -r', stdout=subprocess.PIPE) as p:
  File "/usr/lib64/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/lib64/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ls -r': 'ls -r'
But for a python script that is in the same folder works in both ways as long as you set it executable.
If the script is not executable it produces an error like:
Error:
Traceback (most recent call last): File "subp.py", line 7, in <module> with subprocess.Popen(['./cpt.py'], stdout=subprocess.PIPE) as p: File "/usr/lib64/python3.6/subprocess.py", line 709, in __init__ restore_signals, start_new_session) File "/usr/lib64/python3.6/subprocess.py", line 1344, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) PermissionError: [Errno 13] Permission denied: './cpt.py'
Reply


Messages In This Thread
RE: using subpocess for both reading and writing simultaneously - by killerrex - May-30-2018, 08:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Fastest Way of Writing/Reading Data JamesA 1 2,317 Jul-27-2021, 03:52 PM
Last Post: Larz60+
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 2,753 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  How to run same process simultaneously exploit123 1 2,531 Sep-19-2019, 10:08 AM
Last Post: Gribouillis
  Reading and writing files JakeHoward4 1 1,916 Aug-07-2019, 06:22 PM
Last Post: Yoriz
  Problem with reading and writing to file. darktitan 2 2,406 Jul-20-2019, 06:06 PM
Last Post: darktitan
  Control 2 stepper motor simultaneously jihene 2 4,185 May-08-2019, 05:27 PM
Last Post: DeaD_EyE
  Moving with objects simultaneously kom2 1 3,158 Apr-20-2019, 07:12 PM
Last Post: SheeppOSU
  reading csv and writing csv chawas 2 3,005 Aug-23-2018, 09:28 AM
Last Post: chawas
  controlling multiple server simultaneously. caligola 3 3,747 May-11-2018, 05:44 PM
Last Post: wavic
  How to define two functions run simultaneously within a function? Alberto 4 4,213 Feb-06-2018, 10:08 PM
Last Post: Alberto

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020