Re: program that search string in text file and do something

2017-08-06 Thread Rick Johnson
Grant Edwards wrote:
> Peter Otten <__pete...@web.de> wrote:
> 
> > What we won't do is write a program for you ready to
> > present to your teacher.
> 
> Or if we do, it will be subtly sabotaged in a manner that
> will make it obvious to an experienced Python programmer
> that you didn't write it.  My favorite is to use some
> combination of particularly obscure and obtuse mechanisms
> that will actually product the correct results but do so in
> a way that nobody (including myself a few days later) will
> be able to explain without some intense study.

I would caution the OP against blindly soliciting for free
homework answers on the internet, most especially when the
request includes the deletion of files. Perhaps in this
forum the worst case scenario would be the return of a
scornful rebuke or a heavily obfuscated chunk of abysmal
code. However, in some of the darker, more devious corners
of the web, a naive solicitation such as this could end with
the student's file-system suddenly being relieved of the
burden of carrying around all those extra files. A most
unforgiving lesson in the power and conciseness of the
recursive algorithm.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: program that search string in text file and do something

2017-08-04 Thread Grant Edwards
On 2017-08-04, Peter Otten <__pete...@web.de> wrote:

> What we won't do is write a program for you ready to present to your 
> teacher.

Or if we do, it will be subtly sabotaged in a manner that will make it
obvious to an experienced Python programmer that you didn't write it.

My favorite is to use some combination of particularly obscure and
obtuse mechanisms that will actually product the correct results but
do so in a way that nobody (including myself a few days later) will be
able to explain without some intense study.

-- 
Grant Edwards   grant.b.edwardsYow! It don't mean a
  at   THING if you ain't got
  gmail.comthat SWING!!

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: program that search string in text file and do something

2017-08-04 Thread ast


 a écrit dans le message de 
news:f705c092-de18-4c37-bde1-42316e8de...@googlegroups.com...

On Friday, August 4, 2017 at 12:27:02 PM UTC+3, ast wrote:

 a écrit dans le message de
news:b6cc4ee5-71be-4550-be3e-59ebeee7a...@googlegroups.com...



thanks man! that works


I hope it is not a school homework 


--
https://mail.python.org/mailman/listinfo/python-list


Re: program that search string in text file and do something

2017-08-04 Thread alon . najman
On Friday, August 4, 2017 at 12:27:02 PM UTC+3, ast wrote:
>  a écrit dans le message de 
> news:b6cc4ee5-71be-4550-be3e-59ebeee7a...@googlegroups.com...
> > Hi, I'm new to thing forum and to this programming in python!
> >
> > can someone help me and write me how to write a program that do:
> > - search for a string in certain text file and if it founds the string it 
> > delete the file? and 
> > print something?
> >
> > thanks.
> 
> import os
> pattern = "azerty"
> found = False
> 
> with open("foo.txt", "r") as f:
> for line in f:
> if pattern in line:
> found = True
> break
> 
> if found:
> os.remove("foo.txt")

thanks man! that works
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: program that search string in text file and do something

2017-08-04 Thread Peter Otten
Peter Otten wrote:

> What we won't do is write a program for you ready to present to your
> teacher.

I should have known better :( 


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: program that search string in text file and do something

2017-08-04 Thread Peter Otten
alon.naj...@gmail.com wrote:

> Hi, I'm new to thing forum and to this programming in python!
> 
> can someone help me and write me how to write a program that do:
> - search for a string in certain text file and if it founds the string it
> delete the file? and print something?

Programming is mostly about splitting a complex task into baby steps.

Do you know how to open a file?
Do you know how to iterate over the lines of that file?
Do you know how to search for a string in that line (i. e. another string)?
Do you know how to delete a file?

Work through a Python tutorial or the first chapters of beginner's textbook, 
then try your hand at each of the subtasks outlined above. Consult the 
documentation for missing parts. 

Once you have some code we will help you improve or even fix it.

What we won't do is write a program for you ready to present to your 
teacher.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: program that search string in text file and do something

2017-08-04 Thread ast


 a écrit dans le message de 
news:b6cc4ee5-71be-4550-be3e-59ebeee7a...@googlegroups.com...

Hi, I'm new to thing forum and to this programming in python!

can someone help me and write me how to write a program that do:
- search for a string in certain text file and if it founds the string it delete the file? and 
print something?


thanks.


import os
pattern = "azerty"
found = False

with open("foo.txt", "r") as f:
   for line in f:
   if pattern in line:
   found = True
   break

if found:
   os.remove("foo.txt")




--
https://mail.python.org/mailman/listinfo/python-list