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(). Commented Sep 26, 2019 at 3:00
  • @Selcuk what should i do? 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? 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.

Sign up to request clarification or add additional context in comments.

8 Comments

I got it. I make it in function live_filename(). Do I have to do it again?
@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.
I got it. I will try this. :)
I try this.but error show ==> expected str, bytes or os.PathLike object, not tuple
@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.
|
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 Comments

I got it, but if I have 100 files. what should i do?
@lalin Are all these files in the same folder?
@OverLordGoldDragon Yes in the same folder.
@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 Yes I receiving file paths from a function. output is : ('home', 'Home') ('fun', 'Fun')
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.