Check files that have a certain word - Python (Library os)

Asked

Viewed 126 times

0

I have the code below which I access a certain folder and bring the name of all files with extension . Asp.

import glob
import os

os.chdir('G:\PASTA\SUBPASTA')
for file in glob.glob('*.asp'):
    print(file)

I need to improve this code as follows:

Access file by file contained in folder and identify which files have a certain word within it.

Example:

Which files have the word Bench and then bring the found list, such as:

G:\PASTA\SUBPASTA\Arquivo.asp

G:\PASTA\SUBPASTA\ArquivoXPTO.asp
  • Can read the contents of a file?

  • Not yet Anderson.

  • So start by researching the function open and the library io python native.

  • import glob import os Arq = os.chdir('G: PANALYTICS_PROD Iamdata-u Pesquant_c Sitessg Background') for file in glob.glob('*. Asp'): for info in open(file): #if info.read(). contain a_word('Bench'): #print(file)

  • Anderson, I believe this is the way. I couldn’t evolve the issue of identifying the word inside the file

  • if 'bench' in info.read() would already solve, but let you study the library better to analyze the solution. Good luck.

  • Thank you Anderson. I will create the solution.

Show 2 more comments

1 answer

0

You need to open the file contents first and then perform a read by checking if the word you want is inside the file.

How to Open Python File ?

To open a Python file use the function open(). In this function you must pass the file path (only the name is required if the file is in the same directory as your program ) and the opening mode.

How to read Python files ?

After opening the file you will need to read the contents. For this there are three methods that you can use: read(), readline() and readlines(). In your case, I recommend using the read() to speed things up.

Closing the file.

After opening and reading the file, you should close it using the method close(). After using this method, you will no longer be able to access the contents of this file until it is opened again.

What would my code look like after all this ?

import glob
import os

key = "<Digite a palavra aqui>"

os.chdir('G:\PASTA\SUBPASTA')
for filename in glob.glob('*.asp'):
    print("Verificando o arquivo",filename,"...")
    file = open(filename)
    if key in file.read():
        print("A palavra",key,"existe no arquivo",filename+"!")
    file.close()

I recommend that you study more about the open(), because there are many more things in it that can be useful to you.

  • #With the hints and wrote the following function

  • import glob import os def proc_term_dir(direct, ext, term): """" returns the name of files that have a certain term """ path = os.chdir(direct) for file in glob.glob(ext): for info in open(file, encoding='utf8'): if term in info: print(file)

  • Perfect! You just forgot to close the file, but okay kkk. The important thing is you have understood how to use the =D open

Browser other questions tagged

You are not signed in. Login or sign up in order to post.