How to Open Multiple Files Using "With Open" in Python

How can I open multiple files using with open in Python?

As of Python 2.7 (or 3.1 respectively) you can write

with open('a', 'w') as a, open('b', 'w') as b:
do_something()

(Historical note: In earlier versions of Python, you can sometimes use
contextlib.nested() to nest context managers. This won't work as expected for opening multiples files, though -- see the linked documentation for details.)


In the rare case that you want to open a variable number of files all at the same time, you can use contextlib.ExitStack, starting from Python version 3.3:

with ExitStack() as stack:
files = [stack.enter_context(open(fname)) for fname in filenames]
# Do something with "files"

Note that more commonly you want to process files sequentially rather than opening all of them at the same time, in particular if you have a variable number of files:

for fname in filenames:
with open(fname) as f:
# Process f

How to open multiple files in loop, in python

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.

Opening multiple files for reading/writing in Python using with open , without enumerating and listing each one exclusively?

You can use contextlib.ExitStack as container for file handlers. It will close all opened files automatically.

Example:

filenames = "file1.txt", "file2.txt", "file3.txt", "file4.txt"
with ExitStack() as fs:
file1, file2, file3, file4 = (fs.enter_context(open(fn, "w")) for fn in filenames)
...
file2.write("Some text")

how to open multiple file from single file which is having list of files in python and how to do processing on them?

You're code is almost correct. I guess you messed everything up by using f variable for almost everything. So you assign multiple different things to one f variable. First its single line on outfile, then it's the same line striped, then it's another opened file, and finally you try to use the same f variable outside of it's scope (outside for loop). Try using different variables for all theses beings.

Also make sure you have correct indents (e.g for loop indent is incorrect in your example), and not that regex findall works on string, not file-like object, so second argument of findall should be contentfile.read().

infile = open('bar.txt', "r")
outfile = open('foo.txt', "w")
for f in infile:
name=f.rstrip()
contentfile = open(name,'rw')
#all_matches= re.findall(<define your real pattern here>,contentfile.read())
result = 0 #do something with your all_matches
outfile.write(result)
contentfile.close()
outfile.close()
infile.close()

Open multiple files and store the data in arrays in python

You can create an array with all of the filenames and iterate through those. As long as the files have the data stored the same way and you want to read the same thing in them.

Something like this:

files = ['file1', 'file2', 'file3']

for file in files:
with open(file + '.txt') as datafile:
# Skip the first seven lines, Read the rest of the file

How to open multiple files in a directory

This should do the trick:

import os
Path = "path of the txt files"
filelist = os.listdir(Path)
for i in filelist:
if i.endswith(".txt"): # You could also add "and i.startswith('f')
with open(Path + i, 'r') as f:
for line in f:
# Here you can check (with regex, if, or whatever if the keyword is in the document.)

Apply the same code to multiple files in the same directory

You can define a function that takes the .tif file path and the .csv file path and processes the two

def process(tif_file, csv_file):
pos = mpimg.imread(tif_file)
coord = np.genfromtxt(csv_file, delimiter=",")
# Do other processing with pos and coord

To process a single pair of files, you'd do:

process('pos001.tif', 'treat_pos001_fluo__spots.csv')

To list all the files in your tif file directory, you'd use the example in this answer:

import os

tif_file_directory = "/home/username/path/to/tif/files"
csv_file_directory = "/home/username/path/to/csv/files"
all_tif_files = os.listdir(tif_file_directory)

for file in all_tif_files:
if file.endswith(".tif"): # Make sure this is a tif file
fname = file.rstrip(".tif") # Get just the file name without the .tif extension
tif_file = f"{tif_file_directory}/{fname}.tif" # Full path to tif file
csv_file = f"{csv_file_directory}/treat_{fname}_fluo__spots.csv" # Full path to csv file

# Just to keep track of what is processed, print them
print(f"Processing {tif_file} and {csv_file}")
process(tif_file, csv_file)

The f"...{variable}..." construct is called an f-string. More information here: https://realpython.com/python-f-strings/



Related Topics



Leave a reply



Submit