0

For example, I have some 43000 txt files in my folder, however, I want to read not all the files but just some of them by giving in a range, like from 1.txt till 14400.txt`. How can I achieve this in Python? For now, I'm reading all the files in a directory like

for each in glob.glob("data/*.txt"):
    with open(each , 'r') as file:
        content = file.readlines()
        with open('{}.csv'.format(each[0:-4]) , 'w') as file:
            file.writelines(content)

Any way I can achieve the desired results?

10 Rep
  • 2,217
  • 7
  • 19
  • 33
hyeri
  • 663
  • 9
  • 26

3 Answers3

1

Since glob.glob() returns an iterable, you can simply iterate over a certain section of the list using something like:

import glob

for each in glob.glob("*")[:5]:
    print(each)

Just use variable list boundaries and I think this achieves the results you are looking for.

Edit: Also, be sure that you are not trying to iterate over a list slice that is out of bounds, so perhaps a check for that prior might be in order.

thekid77777
  • 391
  • 1
  • 10
0

If the files have numerically consecutive names starting with 1.txt, you can use range() to help construct the filenames:

for num in range(1, 14400):
    filename = "data/%d.txt" % num
John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

I found a solution here: How to extract numbers from a string in Python?

import os
import re

filepath = './'

for filename in os.listdir():
    numbers_in_name = re.findall('\d',filename)
    if (numbers_in_name != [] and int(numbers_in_name[0]) < 5 ) :
        print(os.path.join(filepath,filename))
        #do other stuff with the filenames
    

You can use re to get the numbers in the filename. This prints all filenames where the first number is smaller than 5 for example.