0

Have some lightweight text search engine. It iterates through the .txt files in one folder, searches for selected term and displays the name of the file. All is based on the functionality of os library:

import os

dirname = '/Users/user/Desktop/test/reports'
save_path = '/Users/user/Desktop/test/folder'

search_terms = ['card']
search_terms = [x.lower() for x in search_terms]
Word = ''.join(search_terms)

for f in os.listdir(dirname):
    with open(os.path.join(dirname,f), "r", encoding="latin-1") as infile:
        text =  infile.read()

    if all(term in text for term in search_terms):
        print (f)
        os.path.join(save_path,f)  

3001017.txt
3003402.txt
3002866.txt
3003763.txt
3004961.txt
3003834.txt
3001986.txt

The search is working good itself but I want as a final operation save the .txt files that were received in the result in another folder save_path For that I trying to use os.path.join(save_path,f) but it seems that it is not working. What will be the right approach to do that?

Keithx
  • 2,994
  • 15
  • 42
  • 71

1 Answers1

2

os.path.join only merges strings to look like a path, and has nothing to do with saving data to a file.

Refer to this example which uses the pathlib module

In [1]: from pathlib import Path

In [3]: p = Path.cwd()

In [4]: p
Out[4]: PosixPath('/home/bluesmonk')

In [7]: p.stem
Out[7]: 'bluesmonk'

In [9]: p.is_dir()
Out[9]: True

In [10]: p2 = p.joinpath('foo') # path does not exist

In [11]: p2.is_dir()
Out[11]: False

Note that creating p2 does not create anything in the file system.

Regarding how to save files, you need to specify the mode as a second parameter, being 'w' the mode for writing. Check the documentation for more info.

In [12]: with open(p.joinpath('foo.txt'), 'w') as saved_file:
    ...:     txt = 'hello world!'
             print(saved_file)
    ...:     saved_file.write(txt)
    ...:     
<_io.TextIOWrapper name='/home/bluesmonk/foo.txt' mode='w' encoding='UTF-8'>


In [13]: ls
 Code/       Documents/         Library/    Public/      Vagrant/
 Desktop/    Downloads/         Music/      snap/        Videos/
 foo.txt     examples.desktop   Pictures/   Templates/  'VirtualBox VMs'/

In [14]: cat foo.txt
hello world!

Your code thus would look like

for f in os.listdir(dirname):
    with open(os.path.join(dirname,f), "r", encoding="latin-1") as infile:
        text =  infile.read()

    if all(term in text for term in search_terms):
        print (f)
        with open(os.path.join(save_path,f), 'w') as sf:
            sf.write(text) 

Note also that pathlib exposes read_text and write_text methods, among others.

bluesmonk
  • 1,237
  • 13
  • 31