-1

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

Stuart
  • 9,597
  • 1
  • 21
  • 30
  • Try using the full path. Or make sure you are running from the correct directory ([`os.getcwd()`](https://docs.python.org/3/library/os.html#os.getcwd). – 001 Sep 24 '20 at 15:03
  • What output did you expect? What files/paths are there in this directory? – Stuart Sep 24 '20 at 15:05
  • If `mydirec` is in the same direcory as the script, do `directory = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'mydirec')`. – ekhumoro Sep 24 '20 at 15:09
  • Just a guess: Maybe try ```'.'``` instead of ```'./mydirec'```? – Timus Sep 24 '20 at 15:10

1 Answers1

-1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Matt
  • 225
  • 2
  • 9
  • I think the question is rather why ```glob()``` doesn't produce the expected output – Timus Sep 24 '20 at 15:35
  • that's my bad then. I read "trying to get data from file" and "this code isn't working" and then was like "but why would you do that in the first place? reading text files is easy, just do this." – Matt Sep 24 '20 at 15:38
  • The title was misleading, indeed – Timus Sep 24 '20 at 15:39