Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iterating over files
#4
something like this
names = ['ligand1.inp','ligand2.inp','ligand3.inp','ligand4.inp']
for i, name in enumerate(names, start=1):
    with open('CONFIG.inp') as cf, open(f'COFIG{i}.inp', 'w') as f:
        for j, line in enumerate(cf, start=1):
            if j == 3:
                line = line.replace('ligand.inp', name)
            f.write(line)
if you show what ligand names look like it may be possible to avoid explicit list of names. Although your approach - reading the full file in memory then replace ligand.inp is perfectly OK if you are sure there is only one occurrence of ligand.inp in the file, I preferred to make sure the replacement is only in line 3.

Also note I don't know if there is better approach from business point of view to achieve your ultimate goal. My example is what you asked for - create multiple config files
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
iterating over files - by clarablanes - Aug-29-2018, 05:57 PM
RE: iterating over files - by buran - Aug-29-2018, 07:05 PM
RE: iterating over files - by clarablanes - Aug-29-2018, 07:55 PM
RE: iterating over files - by buran - Aug-29-2018, 08:17 PM
RE: iterating over files - by clarablanes - Aug-30-2018, 08:55 AM
RE: iterating over files - by Gribouillis - Aug-29-2018, 08:30 PM
RE: iterating over files - by buran - Aug-30-2018, 09:25 AM
RE: iterating over files - by clarablanes - Aug-30-2018, 09:32 AM
RE: iterating over files - by buran - Aug-30-2018, 09:40 AM
RE: iterating over files - by Gribouillis - Aug-30-2018, 09:51 AM
RE: iterating over files - by clarablanes - Aug-30-2018, 09:59 AM
RE: iterating over files - by Gribouillis - Aug-30-2018, 12:16 PM
RE: iterating over files - by clarablanes - Aug-30-2018, 12:32 PM
RE: iterating over files - by buran - Aug-30-2018, 12:34 PM
RE: iterating over files - by clarablanes - Aug-30-2018, 12:43 PM
RE: iterating over files - by buran - Aug-30-2018, 12:51 PM
RE: iterating over files - by Gribouillis - Aug-30-2018, 01:53 PM
RE: iterating over files - by clarablanes - Aug-30-2018, 02:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Iterating Large Files Robotguy 10 5,355 Jul-22-2020, 09:13 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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