0

I have this question I hope you could help me..

Basically I have an app where I open a file, read the word from my file and print that word by console. But I want to print "word no found" in case of no finding the word.

Here's my python :

import re 

file = open("example.txt","r")
content = file.read()
file.close()
car = re.findall('car\s(.*?)\s',open('example.txt','r').read())
toy = re.findall('toy\s(.*?)\s',open('example.txt','r').read())
print car[0]
print toy[0]

here's my text file:

car red

toy green

I'm getting by console this:

red
green

Working great as you can see but if I don't have the word "toy" in my file. There's a possible way to get something like this by console :

Red
No toy found!

Thank you!

1
  • 5
    if toy: print toy[0] else: print 'No toy found' Commented Nov 24, 2016 at 3:24

3 Answers 3

1

Do not open the file in several locations. It is a bad practice. Open in one place and use that object.

content = open('example.txt', 'r').read()
if 'car' in content:
   print("found")
else:
   print("not found")
Sign up to request clarification or add additional context in comments.

Comments

1
import re 

file = open("example.txt","r")

content = file.read()

file.close()

car = re.findall('car\s(.*?)\s',open('example.txt','r').read())

toy = re.findall('toy\s(.*?)\s',open('example.txt','r').read())

if car == []:
   print "No car found"
else:
   print car[0]

if toy == []:
   print "No toy found"
else:
   print toy[0]

Comments

1

Instead of printing toy[0] immediately, use an if-else statement to check if there is a toy before printing to the console. If there is no toy, print "No toy found".

Based on the code sample you supplied, the solution would look like this:

import re 

file = open("example.txt","r")
content = file.read()
file.close()
car = re.findall('car\s(.*?)\s',open('example.txt','r').read())
toy = re.findall('toy\s(.*?)\s',open('example.txt','r').read())
if toy:
    print toy[0]
else:
    print 'No toy found'

if car:
    print car[0]
else:
    print 'No car found'

Note that while this solution may work, there are some improvements that can be made to your code overall. The improvements include:

  • Using the Python with statement to easily close the file you opened
  • Avoid using a variable named find as it is a keyword in Python and may lead to unexpected behavior later in your application
  • Using the data you've saved in the content variable for the regular expression match (so that you don't have to read from the file again).

The improvements will leave you with code that looks like this: import re

#use with statement to open and close file cleanly
with open('example.txt','r') as f: 
    # save file contents in the content variable so you don't need to open the file again
    content = f.read() 

# use data stored in content variable for your regular expression match
car = re.findall('car\s(.*?)\s',content)
toy = re.findall('toy\s(.*?)\s',content)

if toy:
    print toy[0]
else:
    print 'No toy found'

if car:
    print car[0]
else:
    print 'No car found'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.