[Tutor] Re: Referencing a string

Lee Harr missive at hotmail.com
Sun Sep 14 20:13:03 EDT 2003


>I am having problem in using os.system(cmd) function.
>The function does not return the correct result when a string
>variable is given in the pattern field. The code is like this.
>
>cmd = "grep -c pattern " + 'filelist3' + " > " + 'grepdata'
>os.system(cmd)
>
>Instead of looking at the contents of variable pattern, it takes pattern
>as the string to search and searches in the file. The contents of the
>variable pattern is correctly shown when I do a print.
>
>Instaed of variable, when I tried with a fixed pattern like "the",
>it works fine.
>
>Please suggest me the correct method.
>

Hi;

Please limit your line lengths to < 80 characters when posting.

# after this line:
cmd = "grep -c pattern " + 'filelist3' + " > " + 'grepdata'
# print out the result before (or instead of) running it
print cmd
#os.system(cmd)

Often when I do something like this, the potential for disaster
is high (deleting or corrupting important files) so I will print first,
then comment out the print and uncomment the system() call
when I am certain it does what I want.

As it stands now, the only thing you will ever grep for is 'pattern'.

You may want something like this:

pattern = "_some_intersting_pat_"
search_file = "filelist3"
output_file = "grepdata"

cmd = "grep -c '%s' %s > %s" % (pattern, search_file, output_file)

_________________________________________________________________
MSN 8 with e-mail virus protection service: 2 months FREE* 
http://join.msn.com/?page=features/virus




More information about the Tutor mailing list