Python glob does not find any results

415 Views Asked by At

I am trying to extract some data from a text file. Those are the line of code:

directory = './mydirec'
files = glob('{0:s}/*.gather.txt'.format(directory))

I keep receiving [] , so no results. Someone can help me to understand why? Thanks

1

There are 1 best solutions below

3
On

Perhaps I misunderstand your question, but gathering data from a .txt is really really easy. You can open and read a text file with 3 or 4 lines of code:

f = open("file.txt", "r+")
data = f.read()
print(data)
f.close()

and if you're looking for data from specific lines of a .txt you can use:

f = open("file.txt", "r+")
lines = f.readlines()
print(lines[25])
f.close()

or if you want to look for a specific piece of data in a text file in general

f = open("file.txt", "r+")
read = f.read()
data = read
f.close()
if "<specific word you're looking for>" in data:
#react based on the information being in the data.

I don't believe you have to mess with .format or anything like that, you can just use the path of the file, or if its in the same folder as your script, just the filename.