0

I have a python script that will take each file in a directory, iterate through the contents of the file and create an output file for each input file. This output file may have duplicate entries and if it does I would like to take only the unique values similar to the UNIX command

uniq -u input.file > output.file

Although I could use shell scripts to do this I would like to include a line of python that will take only unique values. I know i can do this:

import os
os.system("uniq -u input.file > output.file")

However when I attempt to put this in a loop so that it will make unique all of the files I just made:

for curfile in fs:
    if curfile[-3:]=='out':
        os.system("uniq -u %s > %s") % (str(curfile), str(curfile[:-4] + ".uniq")

I get the following error:

unsupported operand type(s) for %: 'int' and 'tuple'

I have tried a few syntaxes to try to get the variables recognized but can't find a similar enough example on the web. Any suggestions would be greatly appreciated.

5
  • 1
    You've forgotten a ) in os.system("uniq -u %s > %s") % (str(curfile), str(curfile[:-4] + ".uniq"). Commented Oct 29, 2011 at 18:37
  • the parenthesis for os.system should encolose all of the rest of line, shouldnt it? Commented Oct 29, 2011 at 18:38
  • It's not just a missing paren -- you've also got one in the wrong place. Commented Oct 29, 2011 at 18:43
  • Note that ideally, you should be using subprocess.call() instead of os.system(). Commented Oct 29, 2011 at 18:44
  • why is subprocess.call() better? would be glad to use it Commented Oct 29, 2011 at 18:48

1 Answer 1

3

You have

os.system(
             "uniq -u %s > %s"
         ) % ( # The % and this paren should be inside the call to os.system
                 str(curfile), 
                 str(curfile[:-4] + ".uniq")
               # you're missing a close paren here

you need

os.system(
             "uniq -u %s > %s" % (
                                     str(curfile), 
                                     str(curfile[:-4] + ".uniq")
                                 )
         )

First you format the string, then it goes to os.system -- as you have it now, the string goes to os.system then you attempt to % the result, which is an int. (The return code of uniq.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.