-2

I wanted to find every instances of a file under different directories and search for value 0 in each of those files and replace them with 500.

Please find the code below:

!/usr/bin/python

import glob 
import os


a = glob.glob('/home/*/hostresolve')

for i in a:
print i 

=================================

Now that I found all instances of hostresolve file under home. I wanted to search for value 0 and replace them with value 500 in each of these files. I know there is find and replace function in python but I wanted to know how can we use it to output that we got through glob.

Mouse on the Keys
  • 322
  • 1
  • 5
  • 13

2 Answers2

1

As from [Python Docs] (https://docs.python.org/2/library/glob.html) glob.glob returns a list. In your case, its a list of matching files in the directory. hence to replace the required text in the all the files, we should iterative over the list. Accordingly the code would be

import glob
import os

a = glob.glob('/home/*/host*')
for files in a:
  with open(files, 'r') as writingfile:
    read_data = writingfile.read()
  with open(files, 'w') as writingfile:
    write_data = read_data.replace('0', '500')
    writingfile.write(write_data)

Also using "with" to operate on file data is efficient, because it handles close() and flush() automatically avoiding excess code and it has been suggested in previous answers [1] (https://stackoverflow.com/a/17141572/6005652).

Further to reuse or make it more efficient, u can refer to maps (https://docs.python.org/3/library/functions.html?highlight=map#map) as the list of files is an iterable object.

From my understanding, this suffices an answer to your question.

Community
  • 1
  • 1
Sachin Kalkur
  • 33
  • 1
  • 5
  • @GowthamBalachandhiran python works with correct indentations in the code. please check the indentation of second with statement. this means you are reading and writing while file is open.. which is not what i meant in my code.. please check – Sachin Kalkur Jun 13 '16 at 05:33
  • Hi Sachin I corrected the indentation but still same problem – Gowtham Balachandhiran Jun 13 '16 at 07:40
  • from my perspective: If contents 2 of files are changing and one isn't (with same permissions), then please check your file.. is the required string (to be replaced) present in the file.[Also i had tried this with 4-5 files and all had changed].. i can't figure exact problem. and the code is replacing 0 with 500. – Sachin Kalkur Jun 14 '16 at 05:01
0

It worked except for one thing

There are three files instances the code worked for 2 and 3 instance but first instance file remains same.

[root@localhost home]# cat /home/dir1/hostresolve 
O
[root@localhost home]# cat /home/dir2/hostresolve 
500
[root@localhost home]# cat /home/dir3/hostresolve 

500

Please find the code below :

!/usr/bin/python

import glob  
import os


 a = glob.glob('/home/*/hostresolve')


     for files in a:
          print files
          with open(files, 'r') as writingfile:
          read_data = writingfile.read()
             with open(files , 'w') as writingfile:
                  write_data = read_data.replace('0','500')
                  writingfile.write(write_data)

But when I print files I get all instance of the file which means for loop will process all 3 instances and also checked the permission of these files and I found that all 3 have same permissions

shrys
  • 5,860
  • 2
  • 21
  • 36