Python Forum
How to open and write into several files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to open and write into several files
#1
Hi all, since I'm new in Python, sorry for my easy question but I'd like to open new files in a loop lets say 4 new files which are writable and defined in a loop using for i in range(0,4) and then I want to write some data inside of them.

For example, if I had an only file named as controlpointsdata, the below code is ok

f=open("controlpointsdata","w")
but now I have 4 files and I want it to be written as

a=[i for i in range(0,4)]
f=[0 for i in range(0,4)]
for i in range(0,4):
f[i]=open("controlpointsdata_i","w")
f[i].write(a[i])
f[i].close()
The question is how could I open the files named as ctrlptdata_0, ctrlptdata_1, ctrlptdata_2,ctrlptdata_3 since the above code does not work correctly.

Best Regards,
Ahmet
Reply
#2
files = ['ctrlptdata_0', 'ctrlptdata_1', 'ctrlptdata_2', 'ctrlptdata_3']
for num, filename in enumerate(files):
    with open(filename, 'w') as fp:
        fp.write(num)
Reply
#3
(May-07-2018, 12:27 PM)Larz60+ Wrote:
files = ['ctrlptdata_0', 'ctrlptdata_1', 'ctrlptdata_2', 'ctrlptdata_3']
for num, filename in enumerate(files):
    with open(filename, 'w') as fp:
        fp.write(num)

Thank you for your reply @Larz60+ . I have learnt enumerator() function with your help. However, if I have 300 files, producing a files list like that, is so time consuming, is there a way to handle that problem by defining the file names, parametrically ?
Reply
#4
You can use any expression as the first argument to open().
So all can be done into the loop:

for num in range(300):
    with open('ctrlptdata_{}'.format(num), 'w') as fp:
        fp.write(num)
If you have 300 files
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Right way to open files with different encodings? Winfried 2 241 Apr-23-2024, 05:50 PM
Last Post: snippsat
  Open files in an existing window instead of new Kostov 2 330 Apr-13-2024, 07:22 AM
Last Post: Kostov
  open python files in other drive akbarza 1 713 Aug-24-2023, 01:23 PM
Last Post: deanhystad
  How to open/load image .tiff files > 2 GB ? hobbyist 1 2,472 Aug-19-2021, 12:50 AM
Last Post: Larz60+
  Open and read multiple text files and match words kozaizsvemira 3 6,771 Jul-07-2021, 11:27 AM
Last Post: Larz60+
Question (solved) open multiple libre office files in libre office lucky67 5 3,359 May-29-2021, 04:54 PM
Last Post: lucky67
  Can't open files Lass86 5 2,460 Nov-10-2020, 07:18 PM
Last Post: jefsummers
  Using Python to loop csv files to open them Secret 4 2,754 Sep-13-2020, 11:30 AM
Last Post: Askic
  Find specific subdir, open files and find specific lines that are missing from a file tester_V 8 3,621 Aug-25-2020, 01:52 AM
Last Post: tester_V
  What mode should i open the file to write into a *.msh file Dhanya 0 1,747 Jan-09-2020, 08:27 AM
Last Post: Dhanya

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020