0

I am using Python to process over 100.000 text files which are in a folder:

for f in glob.glob("/folder_path/*.txt"):
 with open(f) as inputfile:

Is there a way to start processing from the N-th file in the folder (let's say from the 30.000 th file in the folder), instead of starting over from the 1st file every time in the folder?

I know I can move the files I don't want to process in another file, but since the files are very big it is not a good idea to do that every time, so I would like to have a way to choose which file to start from programmatically...

Yu Hao
  • 115,525
  • 42
  • 225
  • 281
adrCoder
  • 2,975
  • 2
  • 25
  • 49
  • 3
    Can't you store the file list ahead of the for loop. Then, start the for loop at the Nth file? So instead of "for f in glob..." you store the list "files = glob..." and then start the loop at the Nth element of this list? – jesperk.eth Jun 29 '15 at 12:17
  • 1
    you can also `for f in glob.glob(...)[n:]:` – kylieCatt Jun 29 '15 at 12:24
  • @IanAuld write it as a reply and I will accept it. – adrCoder Jun 29 '15 at 13:23

1 Answers1

3

Slice notation can be used in this case:

for f in glob.glob(...)[n:]:
    with open(f) as inputfile:
Community
  • 1
  • 1
kylieCatt
  • 10,094
  • 5
  • 40
  • 51