7

I'm new to python. I'm want to open multiple files in Python. I'm can open each of them with open() function. I'm not sure about formatting.

with open("/test/filename.css", "r") as f:
     s = f.readlines()
     print(s)

I'm can open one file, but I'm not sure how to open multiple files. This is the code that I have. In live_filename() function have many files.

inputfiles = live_filename()
    for live in inputfiles:
        with open("/test/............. .css, "r") as f:

I don't know how to put code formatting in space. and I think the live variable is a tuple can't concatenate str. what should i do?

4
  • Please edit your question to include a sample input by printing the return value of live_filename().
    – Selcuk
    Commented Sep 26, 2019 at 3:00
  • @Selcuk what should i do?
    – user12117808
    Commented Sep 26, 2019 at 3:04
  • @lalin Edit your question to show us the result of print(inputfiles) Commented Sep 26, 2019 at 3:06
  • Possible duplicate of How can I open multiple files using "with open" in Python?
    – Ramesh
    Commented Sep 26, 2019 at 3:09

2 Answers 2

5

Open each just as you did for one, and then append them to a list:

import os

folderpath = r"D:\my_data" # make sure to put the 'r' in front
filepaths  = [os.path.join(folderpath, name) for name in os.listdir(folderpath)]
all_files = []

for path in filepaths:
    with open(path, 'r') as f:
        file = f.readlines()
        all_files.append(file)

Now, all_files[0] holds the first file loaded, all_files[1] the second, and so on.


UPDATE: for all files in the same folder: first, get the folder path (on Windows, like this). Suppose it's "D:\my_data". Then, you can get all the filepaths of the files as in the script above.

8
  • I got it. I make it in function live_filename(). Do I have to do it again?
    – user12117808
    Commented Sep 26, 2019 at 3:27
  • @lalin Just make sure filepaths contains all paths of files you plan to load - you can check via print(filepaths). If so, no need to repeat. Commented Sep 26, 2019 at 3:29
  • I got it. I will try this. :)
    – user12117808
    Commented Sep 26, 2019 at 3:31
  • I try this.but error show ==> expected str, bytes or os.PathLike object, not tuple
    – user12117808
    Commented Sep 26, 2019 at 3:39
  • @lalin If you didn't change your live_filename() and it has the same output, then that is the problem. Just don't use it - instead follow my answer instructions step-by-step. Commented Sep 26, 2019 at 3:44
1

You can do like this:

folder = "..." # Absolute path to folder in which the files reside
files_to_read = [("filename.css","FileName"),( "filename2.css","Filename2")]
for (file, _) in files_to_read:
    filepath = os.path.join(folder, file)
    with open(filepath, "r") as f:
        s = f.readlines()
        print(s)
6
  • I got it, but if I have 100 files. what should i do?
    – user12117808
    Commented Sep 26, 2019 at 3:06
  • @lalin Are all these files in the same folder? Commented Sep 26, 2019 at 3:07
  • @OverLordGoldDragon Yes in the same folder.
    – user12117808
    Commented Sep 26, 2019 at 3:09
  • @lalin Do you want it to be processed in parallel? You are receiving file paths from a function right? Can you show a sample output of live_filename()
    – MjZac
    Commented Sep 26, 2019 at 3:10
  • @MjZac Yes I receiving file paths from a function. output is : ('home', 'Home') ('fun', 'Fun')
    – user12117808
    Commented Sep 26, 2019 at 3:18