2

If anyone could point me in the right direction I'd be really grateful. I am looking to replace the following:

file1 = open ('filepath')
file1.write(data1)
file2 = open ('filepath2')
file2.write(data2)
file3 = open ('filepath3')
file3.write(data3)

With something like this which can be iterated through:

file[i] = open ('filepath')
file[i].write(data[i])

The reason they all need different names is because all the files must be open at once without closing. This is just a requirement of the system.

Is there any way in which this can be done?

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
Robin Hartley
  • 91
  • 1
  • 8

5 Answers5

3
open_files = [open(fname) for fname in ['filepath1', 'filepath2', 'filepath3']]

for fh in open_files:
    fh.write(...)

or

for i, fh in enumerate(open_files):
    fh.write(data[i])
chepner
  • 497,756
  • 71
  • 530
  • 681
2

You can iterate over the file paths using enumerate:

for f in enumerate("fil1","file2","file3"):
    with open(f,"w") as fle:
        fle.write(data[i])

Or zip the file names and data:

for f,d in zip(("fil1","file2","file3",data)):
        with open(f,"w") as fle:
            fle.write(d)

If you want them to stay open store the file objects in a dict:

d = {}
for f,d in zip(("fil1","file2","file3",data)):
        d[f] = open(f,"w")
        f[f].write(d)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

You can use a dictionary.

files = {'filepath1': open('filepath1'), 'filepath2': open('filepath2')}

If you want to generate the dictionary in an iterative way, you can do something like this:

path = 'filepath{0}'
for i in range(10):
    filepath = path.format(i)
    files[filepath] = open(filepath)
Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
0

Well You can do something like:-

filepaths=[List of all your file paths (Ex. "abc.txt", "\c\example\abc.txt")]
fileptr =[]
for file in filepaths :
    fileptr += open(file,'mode')

for fil in fileptr :
    fil.write(data[i])
btechhb
  • 21
  • 1
  • 8
0

Thank you for the suggestions made, I explored around the subject after having looked at the various ways of doing this and came up with a good solution (for my application anyhow).

The trouble I was having was that I'm writing data to a networked csv file with a raspberry pi. If the file is open elsewhere, the pi can no longer access it and data is lost as it can't be recorded on the csv file.

The initial solution which prompted this question was to keep all the files open at once, allowing other users to only open the csv file in read only mode which still allows the pi to write data.This however needs all files to be kept open in the python script and is rather memory intensive if I'm right in thinking,

Therefore, the solution I found was to make the file read only all the time I wasn't writing to it with the pi and then only making it writable just as I was adding information to the csv file.

I used the code explained here: How to remove read-only attrib directory with Python in Windows?

Many thanks

Community
  • 1
  • 1
Robin Hartley
  • 91
  • 1
  • 8