This script allows one to exchange two columns in a file.And it do so for all the files in a directory. The python script is :
----------------------------
#script to read and execute column exchange for all files in the directory
import os
listing=os.listdir("/home/K/Documents/PY/column_exchange")
for infile in listing:
print infile
if infile !="column_interchange_script.py":
fi=open(infile).readlines()
fo=open(infile +'_output','w')
space=" "
for line in fi:
first,second=line.split()
fo.write('%s%s%s\n'%(second,space,first))
else:
print "This is not interesting file"
----------------------------------------------------
Some expert might simplify it a lot more, but for new python user like me, this is a big leap.
So, how does it work:
Create a directory where in all the files for which column exchange need to be done are stored.
Keep this python script also in the same directory.
The path shown in bold need to be replaced with the proper path of the directory.
$python column_interchange_script.py command shoulf carry out column exchange for all the files in the directory.
One problem I was facing was the script was reading itself also and collapsing with an error. So I added exception using "!=" returning result for it with else statement.
One precaution: if you modify the script using editor, then it creates a temprary file column_interchange_script.py~ sometimes. This could create problem. script reads this file also and collapses. So either modify it in terminal or delete this file.
Why I needed this?
The Umbrella Sampling calculations yield files with values in two columns. For plotting individual histograms corresponding to each file we need to exchange columns. Number of such files might be large enough to motivate you to write a script to do so.
I have few things in mind to improvise on it, but since my task is fulfilled with this script, I might not modify it any further
----------------------------
#script to read and execute column exchange for all files in the directory
import os
listing=os.listdir("/home/K/Documents/PY/column_exchange")
for infile in listing:
print infile
if infile !="column_interchange_script.py":
fi=open(infile).readlines()
fo=open(infile +'_output','w')
space=" "
for line in fi:
first,second=line.split()
fo.write('%s%s%s\n'%(second,space,first))
else:
print "This is not interesting file"
----------------------------------------------------
Some expert might simplify it a lot more, but for new python user like me, this is a big leap.
So, how does it work:
Create a directory where in all the files for which column exchange need to be done are stored.
Keep this python script also in the same directory.
The path shown in bold need to be replaced with the proper path of the directory.
$python column_interchange_script.py command shoulf carry out column exchange for all the files in the directory.
One problem I was facing was the script was reading itself also and collapsing with an error. So I added exception using "!=" returning result for it with else statement.
One precaution: if you modify the script using editor, then it creates a temprary file column_interchange_script.py~ sometimes. This could create problem. script reads this file also and collapses. So either modify it in terminal or delete this file.
Why I needed this?
The Umbrella Sampling calculations yield files with values in two columns. For plotting individual histograms corresponding to each file we need to exchange columns. Number of such files might be large enough to motivate you to write a script to do so.
I have few things in mind to improvise on it, but since my task is fulfilled with this script, I might not modify it any further
Comments
Post a Comment