Skip to main content

Editing multiple files simultanoeusly

I needed to delete all the lines in a set of files except for for the first forty. Since the number of files was big, I used this script in python to do the job:

--------------------------------

!/usr/bin/python
#script to read files and delete lines
import os
listing=os.listdir("/home/kjoshi/Documents/PY/useful/file_edit")
for infile in listing:
 print infile
 if infile == "files_line_delete.py":
   print "This is NOT a good file"
 else:
  fi=open(infile).readlines()
  fo=open(infile +'_output','w')
  del fi[40: ]
  fo.writelines(fi)

---------------------------------

Task is simple. Keep all the files needed to be edited in one folder along with this script.
Substitute the path shown in bold with proper path of your folder.
Using command
$python files_line_delete.py
you get output files with each file having first forty lines.

Comments

Popular posts from this blog

Material Studio - selective molecule movement

So, working with Material Studio is awesome. You get to handle bigger, complicated and even unimaginable system. But I am facing problems in very minute functioning. I recently faced two problems, but managed to resolve on my own. Somehow, tutorials doesn't help in resolving them and you end up doing some exercise on your own to get the solution. Among these, the problem of bringing two molecular structures in one window and then adjusting one molecule by moving it around, while other structure remains untouched, as it is. Actually, this should not be a problem to do. There is a simple way of doing this. Select the molecule to be moved and then there is a symbol (button) to select, which allows you to selectively move that molecule only. This is the same way it can be done in Material studio too.  But among all the dozens of symbols, the new person handling MS, kind of gets lost. So, what he does is check the tutorials, for any help. But the tutorials don't talk about suc...

How would COVID bahave in August - few projections

Entering August with COVID by side   Entering in the month of August, COVID is still here by our side. Situation seems grim, worse than earlier, as number of active cases as well as the count of deceased patients are increasing day by day. Yesterday we crossed the mark of 50,000 cases in a day. Closely competing with Brazil with US counts also in range now. On the other side, few other numbers like reduced mortality rate to 2.18%, recovery rate of 64.6% and slightly increased doubling rate of 21 days can reduce the anxiety. My projections of 16.75 lakhs for India might be crossed by coupe of thousands of more cases after today’s count is added. This number would reach to a huge count of 28.5 lakhs by August 15. Maharashtra just doesn’t like competition. Just like what Delhi tried few days back, Andhra Pradesh also managed to cross Maharashtra’s per day count for2 consecutive days. Seems like not a good idea. Maharashtra immediately went to the next zone. The state which w...

COVID projections till July end

This last week seems terrible. The kind of spurt we saw in last many days was unexpected. Yesterday's count was 45000+. I did not expect. Are coming days going to report bigger numbers than this? We need to understand the cycle it follows. Mumbai is a good example. Since beginning Mumbai was heading the race. While other cities were reporting the count in 10's or 100's, Mumbai was reporting in thousands. back then, as much as 60-70% of Mahrashtra's total count was coming from Mumbai. Now after a period of almost 3 months, Mumbai is lagging behind. Pune, Kalyan, Thane have overtaken Mumbai. The famous hotspots of Dharavi, Dadar, Sion, Worli are longer uttered in the news. Of course, the counts are still there, but they do not attract any more attention. This is a cycle which will be followed in every new red zone area. The point is how big spike we allow the zone to get and how quickly we nab it. Delhi at one point started competing with Mumbai/Maharashtra. But, it h...