Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iterating over files
#11
I did the change but it says the same.
File 'CONFIG_iterator.py", line 3
with open('CONFIG.inp') as cf, open('CONFIG{}inp'.format(i), 'w') as f:
^
SyntaxError: invalid syntax
Regarding to the version, I have to work in the university server because the programs I use are installed there

(Aug-30-2018, 09:51 AM)Gribouillis Wrote: Yes, upgrade to python 3 before you do anything. Here is a complete code for python 3. You only need to save it in the same directory as CONFIG.inp and run it (you can replace the 'names' list by your own names list)

import io

with open('CONFIG.inp') as infile:
    lines = infile.readlines()
lines[2] = lines[2].replace('ligand.inp', '{ligand}.inp')

with io.StringIO() as f:
    f.writelines(lines)
    template = f.getvalue()

names = ['foo', 'bar', 'baz']

for idx, name in enumerate(names, 1):
    with open('CONFIG{}.inp'.format(idx), 'w') as outfile:
        outfile.write(template.format(ligand=name))

Thank you for your code but I think that the problem is that I have to work in the university server and the installed version is 2.6.6.

Dear buran and Gribouillis,
I have tested your codes in my machine with python 3 and they work perfectly!. So,I will do this iteration in my machine and then continue with the university server. Thank you very much you both for your fantastic work.
clara
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