0

I am running MacOS if that helps this at all

I am planning to make a database, with a general script, to manage the entries.

One of the options is to search for an entry (aka a person).

here is a template of what the first string of each entry will be:

n: Name fn: FamilyName a: dd.mm.yyyy eid: XXXXXXXXX e: Active/Inactive

I need a script that'll search every file in the folder (where the entries are stored, mine is " /Database " ) for that strings

I made a simple script that asks you for the person you are looking for using the keywords

Name = input("[Name] ")

FamilyName = input("[Family Name] ")

DOB = input("[Date of Birth] ")

EmployeeID = input("[Employee ID] ")

Email = input("[Email] ")

Status = input("[Status] ")


output = " n: " + Name + " fn: " + FamilyName + " a: " + DOB + " eid: " + EmployeeID + " e: " + Status

I'm also using a script from another StackOverflow post click here: I modified it a little to fit my variables.

dir = "/Users/user/Project/Database"
directory = os.listdir(dir)

searchstring = output

for fname in directory:
   if os.path.isfile(dir + os.sep + fname):
     # Full path
     f = open(dir + os.sep + fname, 'r')

     if searchstring in f.read():
        print('found string in file %s' % fname)
     else:
        print('string not found')
     f.close()

I run the file and it gives me this error:

Traceback (most recent call last): File "/Users/user/project/new.py", line 23, in if searchstring in f.read(): File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte

I hope this isn't too much of a problem,

Suggestions on how to redesign my program are welcome

I think I might want to redesign the first line thing, so that searching for files matching the search requirements can be easier, or just completely remove the first line thing, and maybe use metadata? I don't know

James
  • 32,991
  • 4
  • 47
  • 70
pico
  • 35
  • 7
  • Suspect that you're reading some file that you're not expecting. There can be hidden files in the directory. Can you `print(f)` so that we can see the filename you're reading from? On Mac OS, there's usually a `.DS_Store` hidden file in each directory. – CadentOrange Apr 08 '22 at 07:09
  • I've checked for hidden files in the directory using cmd + shift + "." and nothing was there except the entries I have so far – pico Apr 08 '22 at 07:16
  • first you should use `print(fname)` to get which file makes problem. It seems you read file which doesn't use `UTF-8` but other encoding or it is not text file (so yo shouldn't read it). – furas Apr 08 '22 at 12:02

1 Answers1

0

I think I found my answer, I'm going to use TinyDB github link, it simplifies this whole process,

Bye!

pico
  • 35
  • 7