Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Help with strings (http://www.programmingforums.org/showthread.php?t=15752)

seVor May 2nd, 2008 12:58 PM

Help with strings
 
Ok so I am trying to created a app that will loop through a folder and delete files with names that contains key words (ie: 'copy'). I have done some research and tried a few things but cant quite get it to work. Is there a way to check if 'copy' is in a string. Like a .contains() or something. Thanks for your help

Freaky Chris May 2nd, 2008 1:09 PM

Re: Help with strings
 
indeed there is

:

  1. teststring = "hello there"
  2. z = teststring.find("there")
  3. print z


What will be printed out is there position of the first character of the substring "there".

Hope this help,

Chris

Sane May 2nd, 2008 3:18 PM

Re: Help with strings
 
It gets even simpler than that. You can use the "in" operator to test for the occurrence of a string within another string. For example, the following program will search for the string "quey" within every file inside all directories and subdirectories of "fdir".

:

  1. import os
  2.  
  3. query = "localhost".lower()
  4. fdir = 'aaron_storage'
  5.  
  6. for root, dirs, files in os.walk(fdir):
  7.     for file in files:
  8.         fname = os.path.join(root, file)
  9.         if query in open(fname, 'r').read().lower():
  10.             print '\n' + fname
  11.     print '.',
  12.  
  13. raw_input('Done')


seVor May 5th, 2008 2:16 PM

Re: Help with strings
 
SWEET.... You all are gods!! and I am a newb.... lol

Freaky Chris May 5th, 2008 2:17 PM

Re: Help with strings
 
Gods, nah God yes (Sane) fool who likes to think he knows something about programming (me) lol.
Glad everything is working for you, if it is of course

Chris

seVor May 5th, 2008 2:21 PM

Re: Help with strings
 
Yeah.. I am a .net guru but falling head over heals for linux. PHP and python are awsome.

Freaky Chris May 5th, 2008 2:24 PM

Re: Help with strings
 
Personally unfortunatley i know little of any language, i just know some Python, C++, Java, VB.NET, PHP, JavaScript. But i kinda enjoy knowing a little in each, i hope to be able to gain a better understanding in one area though.

As well as do alot of work on algorithms its just finding time inbetween college, hopefull i'll be able to build on things when i reach uni in a specialized language most likely Java.

Chris

seVor May 5th, 2008 2:26 PM

Re: Help with strings
 
well sounds like you have a good start. Just keep up the learning and it will be great. I recommend also learning MySQL

Freaky Chris May 5th, 2008 2:29 PM

Re: Help with strings
 
Indeed i do need to look into SQL not just specifically for MySQL but for all database software, once again though it is finding time. I use to know the basics of it when i first began to look into PHP in detail but since then i have lost track of it due to the fact i don't use PHP all that often and what little i did got pussed aside for GCSE exams and then college

thanks for the tip though

Chris

seVor May 5th, 2008 2:39 PM

Re: Help with strings
 
yeah... I know MSSQL but its not a free solution. You can pretty much get the gist with MySQL. I am wanting to learn how to access a MySQL database via python. I have some ideas for cracking the md5 ecryption. I know that I will prob never crack it but hey, it might be fun to give it a shot.


All times are GMT -5. The time now is 12:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC