0

Find a specific term in all files using python

How to find a specific term in all files of the local disk using python find a term in all files using python find string in all files using python Search string in all files of directory or subdirectories Search files with specific string/term using python print path of files with specific string/term using python

Problem Statements:

  • How to find a specific term in all files of the local disk using python
  • find a term in all files using python
  • find string in all files using python
  • Search string in all files of directory or subdirectories
  • Search files with specific string/term using python
  • print path of files with specific string/term using python

Sometimes we forgot a file path but we know that we have done the same work before, We just have some clues that the file contained a specific term or string, So how can we use the clues of string or term to find the path of that file? So today I will tell you how to search a specific string/term in all files using python of the current directory or subdirectories.

So let get started our article of how to search a file with specific term/string.

CODE(Find a specific term in all files using python):

import os
from fnmatch import fnmatch

term = input("Enter term to find in files : ")
found = 0
formats = ['.txt','.csv','.docs','docx']
for path, subdirs, files in os.walk(os.getcwd()):
    for name in files:
        if name.lower().endswith(tuple(formats)):
            path = os.path.join(path, name)
            try:
                f = open(path)
                for line in f.readlines():
                    if term in line:
                        print(path)
                        found = 1
                        break
            except Exception as e:
                pass

if found == 0:
    print("No file found with term = ",term)

DEMO:

Save the python file in the directory where you want to search the files with a specific term/string and just run it, It will ask for a term to search and then prints all files with that input string/term, If any file does not contain that term/string then it will print a message

"No file found with term = "{term}

If you have another effective way of doing this task. To use Python to find files containing text / strings or specific terms, share in the comments section.

If you have also done the same in python or any other language the construction site. Share it in the comment section to help others

keep visiting : shaadcoding

0

check string is a valid float python(easy solution)

check if the string is a valid floating number in python

TOPIC – check string is a valid float python

problem statements:

  • What is the best possible way to check if a string can be represented as a number in Python?
  • How to check a string is float or int in python?
  • How to check if a string is a valid float in Python?
  • check if the string is a valid floating number in python

Method 1(check if the string is a valid floating):

def isValidFloat(string):
#Try to convert argument into a float
try:
float(string)

return True
except ValueError:
return False

a_string = "5.66"
a_string1 = "5.66g"

is_valid_float = isValidFloat(a_string)
is_valid_float1 = isValidFloat(a_string1)

print(is_valid_float)
print(is_valid_float1)

output:

True
False

Method 2(check if the string is a valid floating):

def isValidFloat(string):
#replacing first . with empty string
string = string.replace(".","",1)
if string.isdigit():
return True
return False

a_string = "5.66"
a_string1 = "5.66g"

is_valid_float = isValidFloat(a_string)
is_valid_float1 = isValidFloat(a_string1)

print(is_valid_float)
print(is_valid_float1)

output:

True
False

If you have another effective way to accomplish this task. To check if the string is a valid floating point number in Python, share it in the comment section.

If you have also done the same in python or any other language it works . Share it in the comments section to help others

keep visiting : shaadcoding

2

scrape free stock images from websites using python

scrape free stock images from websites using python

Topics : scrape free stock images from websites using python , scrape images from the website , scrape images using python

problem statements:

  • scrape images from the website
  • scrape images using python
  • How to scrape images from websites
  • images scraping using beautiful soup and requests
  • python images web scraping
  • downloading images using python
  • image scraping
  • image scraping python

scrape free stock images from websites using python – Photos are being used more frequently on websites, blogs, slideshows, and ebooks. The need for photos has increased dramatically and more people are using stock photo agencies to buy their photos and images, protect their companies from copyright issues and utilize the many options that stock photo agencies offer.

If we need free stock images in bulk then downloading it from website is much time consuming and not a easy task,

So can we use python to automate the downloading process of free stock images by writing a few lines of code?

The answer is Yes, Python urllib and Beautiful Soup module allow us to scrape/download images from websites. So I will tell you how to use python to automate the boring stuff of downloading images manually, So without wasting the time let get started our article scrape free stock images from websites using python,

Beautiful Soup is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves programmers hours or days of work.

Make sure you have downloaded and installed python if you haven’t download python yet then follow the tutorial to download python :

Downloading and setting up python

Once you have down with downloading and setting up python Then Next Thing you need to download the Python urllib and Beautiful Soupmodule.

Module You need to scrape free stock images from websites using python?

Just open command prompt and run the following commands

pip install urllib3
pip install bs4

After installing urllib3 Next is to create a python project, If you still don’t know how to create a python project then follow the tutorial link below :

Creating project in python

After this paste the code below in that python file

CODE:

import urllib.request
import bs4 as bs

images_scraped=1 
mylist=[] 
term = input("Enter Search term : ")
url = 'https://www.istockphoto.com/search/2/image?phrase='+term 
html = urllib.request.urlopen(url)
soup = bs.BeautifulSoup(html,'lxml') 
for res in soup.findAll('img'):
     try:
         
         num = res.get('src')
         if num in mylist:               
             continue
         else:
             URL = res.get('src')      
             urllib.request.urlretrieve(URL, term+str(images_scraped)+".jpeg")    
             num = num.split('-')
             num = num[-1]
             
             mylist.append(num)
             print(" item ",images_scraped)
             images_scraped+=1
     except Exception as e:     
         pass
         print(e)

After running this code. It will input the search time and auto-downloads all available images for that term in the current working directory.

if you can answer the following question that will be helpful for others.

Q1. How can we limit the number of images scraped/downloaded?

Q2. How can we change the directory for images scraped/downloaded?

If you have another efficient way to perform this task of scraping free stock images from websites using python. Please share in the comment section.

DEMO:

Hope you like the article scrape free stock images from websites using python

keep visiting : shaadcoding

1

copy content of one file to another using Python

copy content of one file to another using Python

other statements for same problem:

  1. Python Program to Copy the Contents of One File into Another
  2. The program copies the contents of one file and writes it into another.
  3. Python – Copy contents of one file to another file
  4. Copying from one text file to another using Python

copy content of one file to another using python – “Today’s Topic”

How to copy from one text file to another using python?

Suppose we have a file and we want to save the content of this file in another for backup so, how we can do this using python?

Python is developed under an OSI-approved open source license, making it freely usable and distributable, even for commercial use. Python’s license is administered by the Python Software Foundation.

python allows us to do this in quick and efficient manner , So let get started…

Make sure you have downloaded and installed python if you haven’t download python yet then follow the tutorial to download python :

Downloading and setting up python

we will have a file, in our case file name “file1.txt” and we will copy the content of “file1.txt” to another file named “file2.txt”. Copy the line below and create a file “file1.txt” and paste these line it, you can also create your own testing file with any other content in it, you can also give a name to this file according to you but please make sure to also change the file name in code too accordingly, Still confusion? drop your question in the comment section, i will try my best to solve your query as soon as possible

file1.txt :

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5

The Simples way of Copying content of “file1.txt” to “file2.txt” is as follows :

PYTHON CODE:

with open("file1.txt") as file1:
with open("file2.txt", "w") as file2:
for line in file1:
file2.write(line)

After running the above code “file2.txt” will be created in same working directory with all content of “file1.txt”

file2.txt:

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5

If you have another effective way to accomplish this task. About using Python to copy content from one file to another, then share it in the comment section.

If you have also done the same in python or any other language works . Share it in the comments section to help others

DEMO:

keep visiting : shaadcoding