Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opening multiple files in Python [closed]

How can I open many files at same time in the python programming language to run my program?

I have about 15 files, just now I have worked with one of them like below:

f=open("Exemplo_1.txt","rU")
like image 529
Dinna Lee Avatar asked Oct 16 '12 12:10

Dinna Lee


People also ask

Can you open multiple files in Python at once?

Python provides the ability to open as well as work with multiple files at the same time. Different files can be opened in different modes, to simulate simultaneous writing or reading from these files.

How many files can Python open at once?

Hence, there can be at most 95141 possible file descriptors opened at once. To change this use: where 104854 is max number which you want. I agree with everyone else here.


2 Answers

You could use a combination of glob and fileinput

import fileinput
from glob import glob

fnames = glob('Exemplo_*.txt')
for line in fileinput.input(fnames):
    pass # do whatever
like image 189
Jon Clements Avatar answered Oct 15 '22 09:10

Jon Clements


f1=open("Exemplo_1.txt","rU");  
f2=open("Exemplo_2.txt","rU");  
...  
f15=open("Exemplo_15.txt","rU");

You're basically creating File objects to get access to the files.

like image 32
venkatKA Avatar answered Oct 15 '22 10:10

venkatKA