Python Forum

Full Version: iterating over files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
(Aug-30-2018, 09:59 AM)clarablanes Wrote: [ -> ]I have to work in the university server and the installed version is 2.6.6.
There could be several versions installed on the same machine, the default being 2.6.6. What is the server's operating system?
It is linux
try python3 -V
it will show which python3 version is installed
with python3 -v it says: command not found
with python2 -v it says a lot of things but finally: Python 2.6.6
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17(] on linux2
some linux distros have both python2 and python3 intsalled (python2 being the system one), but not Red Hat, so no python3 in your case
You could perhaps install python 3 in you universty's user account by installing pyenv first, see here for example.
I will try it!. Thank you for your suggestions.
Pages: 1 2