Python Forum
[SOLVED]passing a lot of information to subprocess.call()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED]passing a lot of information to subprocess.call()
#1
Hi,
I have a long list of names and phone numbers in the following format:
Quote:0100 12345
Harry horse
0100 43234
Jane Jones
0121 67574
Mary Long
0123 26927
Bob Short
I call this file first.txt
What I am doing is create a new file which lists each name and records at the side of it how many times their name appears in the original list.
What I have been doing is using a simple python script to generate a file I call second.txt.
This just contains the names.
From that I run the following:
cat second.txt|sort|uniq>third.txt
This generates another file which has each name in alphabetical order once.
Another python script then reads through third.txt and for each line reads through second.txt and to count how many times that name appears. After each search it writes to a new file this number with the name beside it.

So what I wanted to do is to make one python script to do it all from start to finish rather than running the 3 separate commands. I tried os.system and then subprocess to incorporate the sort and uniq commands line but I cannot seem to get it to work. What ever I have tried gives syntax errors.
For example
cmd = "'cat', /home/norman/khconf/second.txt, '|sort', '|uniq', '>', /home/norman/khconf/third.txt"
subprocess.call([cmd])
Error:
Traceback (most recent call last): File "./getnames.py", line 21, in <module> subprocess.call([cmd]) File "/usr/lib/python3.5/subprocess.py", line 557, in call with Popen(*popenargs, **kwargs) as p: File "/usr/lib/python3.5/subprocess.py", line 947, in __init__ restore_signals, start_new_session) File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: "'cat', /home/norman/khconf/second.txt, '|sort', '|uniq', '>', /home/norman/khconf/third.txt"
so then I tried:
cmd = "'/bin/cat', /home/norman/khconf/second.txt, '|/usr/bin/sort', '|/usr/bin/uniq', '>', /home/norman/khconf/third.txt"
subprocess.call([cmd])
gave
Error:
Traceback (most recent call last): File "./getnames.py", line 21, in <module> subprocess.call([cmd]) File "/usr/lib/python3.5/subprocess.py", line 557, in call with Popen(*popenargs, **kwargs) as p: File "/usr/lib/python3.5/subprocess.py", line 947, in __init__ restore_signals, start_new_session) File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: "'/bin/cat', /home/norman/khconf/second.txt, '|/usr/bin/sort', '|/usr/bin/uniq', '>', /home/norman/khconf/third.txt"
Can someone point me in the right direction please?
Reply


Messages In This Thread
[SOLVED]passing a lot of information to subprocess.call() - by Barrowman - Jan-26-2018, 06:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  continue if 'subprocess.call' failes tester_V 11 5,443 Aug-26-2021, 12:16 AM
Last Post: tester_V
  printing out the contents aftre subprocess.call() Rakshan 3 2,869 Jul-30-2021, 08:27 AM
Last Post: DeaD_EyE
  subprocess call cannot find the file specified RRR 6 17,028 Oct-15-2020, 11:29 AM
Last Post: RRR
  Why wont subprocess call work? steve_shambles 3 2,765 Apr-28-2020, 03:06 PM
Last Post: steve_shambles
  subprocess.call - Help ! sniper6 0 1,565 Nov-27-2019, 07:42 PM
Last Post: sniper6
  Mock call to subprocess.Popen failing with StopIteration tharpa 7 6,064 Nov-08-2019, 05:00 PM
Last Post: Gribouillis
  Echo call to VLC bash using subprocess.Popen on Linux LuukS001 17 9,931 Jan-07-2019, 03:58 AM
Last Post: LuukS001
  Why is subprocess.call command not working? zBernie 5 10,880 Nov-19-2018, 11:11 PM
Last Post: snippsat
  Help with subprocess..call oldcity 1 2,615 Sep-28-2018, 03:59 PM
Last Post: volcano63
  subprocess.call salmaankamil 1 2,774 Jul-27-2018, 06:50 PM
Last Post: woooee

Forum Jump:

User Panel Messages

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