473,411 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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 1706


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: MyHaz | last post by:
OK i find this a quark in string.find that i think needs some consideration. Normally if a substring is not in the searched_string then string.find returns -1 (why not len(searched_string) + 1,...
6
by: JJA | last post by:
I would like some advice on a data and query problem I face. I have a data table with a "raw key" value which is not guaranteed to be valid at its source. Normally, this value will be 9 numeric...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
2
by: Derek Jones | last post by:
I am currently working on an application for our Business Office to easily search for new budget codes that we have to establish due to an administrative system conversion we are currently...
29
by: Ajay | last post by:
Hi all,Could anybody tell me the most efficient method to find a substr in a string.
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
14
by: micklee74 | last post by:
hi say i have string like this astring = 'abcd efgd 1234 fsdf gfds abcde 1234' if i want to find which postion is 1234, how can i achieve this...? i want to use index() but it only give me the...
2
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
I am usign the code below to find the current row and cell. Which works OK. Then hwat I want to do is get the value of each other cell in the row. So below I am trying to get at least one values....
4
by: jelle | last post by:
the subject pretty much says it all. if I check a string for for a substring, and this substring isn't found, should't the .find method return 0 rather than -1? this breaks the if...
4
by: =?Utf-8?B?VG9yZW4gVmFsb25l?= | last post by:
Was editing code, am getting the following errors } expected Type or namespace definition, or end-of-file expected Eyes crossed cannot find code below! using System; using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.