469,935 Members | 1,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,935 developers. It's quick & easy.

trying to find a substring in a string

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 27 '08 #1
1 1624


ba************@gmail.com 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

Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by MyHaz | last post: by
108 posts views Thread by Bryan Olson | last post: by
29 posts views Thread by Ajay | last post: by
14 posts views Thread by micklee74 | last post: by
2 posts views Thread by =?Utf-8?B?Q2hyaXM=?= | last post: by
4 posts views Thread by =?Utf-8?B?VG9yZW4gVmFsb25l?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.