trying to find a substring in a string

Discussion in 'Python' started by barnacle.steve@gmail.com, Jun 21, 2008.

  1. Guest

    I'm trying to write what should be a simple script in Python, which
    I've never ever used before.

    Essentially, I have a text file that has a list of full path file
    names to other files, separated by carriage returns.
    Contents of first file:
    c:\blah.txt
    c:\blah1.txt
    c:\blah2.txt

    The goal is for the user to specify another file, and then search the
    specified file for instances of files from the first file.
    Contents of user specified file:
    file = "c:\blah.txt"
    file = "c:\blah1.txt"

    My goal is for the program to tell me that it found c:\blah.txt and c:
    \blah1.txt.

    I've read the contents of the existing file into an array, where each
    element is a line from the file. I did the same thing with the user
    specified file. I thought it would be a simple nested for loop to find
    them, but I'm having no luck finding the strings. The string find
    method doesn't do it for me. I've tried regular expressions, but it
    never finds a match. I think it doesn't like when I do
    re.compile(variableName), but without a debugger I have no way to tell
    what's going on at all.

    I keep telling myself this should be really simple, but I'm ready to
    jump out a window.

    Any help would be greatly appreciated. Thanks in advance.
     
    , Jun 21, 2008
    #1
    1. Advertising

  2. Terry Reedy Guest

    wrote:
    > I'm trying to write what should be a simple script in Python, which
    > I've never ever used before.
    >
    > Essentially, I have a text file that has a list of full path file
    > names to other files, separated by carriage returns.
    > Contents of first file:
    > c:\blah.txt
    > c:\blah1.txt
    > c:\blah2.txt
    >
    > The goal is for the user to specify another file, and then search the
    > specified file for instances of files from the first file.
    > Contents of user specified file:
    > file = "c:\blah.txt"
    > file = "c:\blah1.txt"
    >
    > My goal is for the program to tell me that it found c:\blah.txt and c:
    > \blah1.txt.
    >
    > I've read the contents of the existing file into an array, where each
    > element is a line from the file.


    Put each stripped (to delete \n) line into a set. Then parse out the
    filenames and check that they are in the set. Something like

    def getname(line): <whatever)

    s=set(line.strip() for line in open('allfiles.txt', 'r'))
    for line in open('paths.txt'):
    if getname(line) not in s:
    return '%s not found'%line
    else:
    return 'all found'

    tjr
     
    Terry Reedy, Jun 22, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. juli
    Replies:
    3
    Views:
    8,912
    Shiva
    Dec 6, 2004
  2. Ajay

    progrm to find a substring in a string

    Ajay, Mar 22, 2006, in forum: C Programming
    Replies:
    28
    Views:
    864
    Default User
    Mar 23, 2006
  3. fishfry

    Find location of substring in string?

    fishfry, Sep 24, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    126
    Bart Lateur
    Sep 24, 2004
  4. Replies:
    3
    Views:
    235
    Sherm Pendley
    Aug 3, 2005
  5. SM
    Replies:
    4
    Views:
    229
Loading...

Share This Page