Hi,
I have a long list of names and phone numbers in the following format:
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:
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
gave
I have a long list of names and phone numbers in the following format:
Quote:0100 12345I call this file first.txt
Harry horse
0100 43234
Jane Jones
0121 67574
Mary Long
0123 26927
Bob Short
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:
1 |
cat second.txt|sort|uniq>third.txt |
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
1 2 |
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:1 2 |
cmd = "'/bin/cat', /home/norman/khconf/second.txt, '|/usr/bin/sort', '|/usr/bin/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: "'/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?