-3

How can I select multiple files in a folder using python for example I am coding using colab and I would like to read several txt file into the system

  • Possible duplicate of [How can I open multiple files using "with open" in Python?](https://stackoverflow.com/questions/4617034/how-can-i-open-multiple-files-using-with-open-in-python) – MyNameIsCaleb Apr 22 '19 at 17:53

1 Answers1

0

If you want to do same operations on different files you can do this:

import os
dir = "\home\my_dir"#<your_dir>
files = os.listdir(dir)
for file in files:
    if file.endswith(".txt"): 
        with open(dir+file, 'r') as current_file:
            #<do interesting stuff>

If you require different things to do in each file, you'll have to make separate file object for each file.

file1 = open('file1.txt', 'r')
file2 = open('file2.txt', 'r')
and so on
Nevus
  • 1,307
  • 1
  • 9
  • 21