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
#12
(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?
Reply
#13
It is linux
Reply
#14
try python3 -V
it will show which python3 version is installed
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
#15
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
Reply
#16
some linux distros have both python2 and python3 intsalled (python2 being the system one), but not Red Hat, so no python3 in your case
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
#17
You could perhaps install python 3 in you universty's user account by installing pyenv first, see here for example.
Reply
#18
I will try it!. Thank you for your suggestions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Iterating Large Files Robotguy 10 5,194 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