PLUTO
file.py
Go to the documentation of this file.
1 """ file.py
2 
3  Useful utilities for file editing/manipulation
4 
5  by Andrea Mignone
6 
7  Last modified Feb 2nd, 2008
8 """
9 
10 import string
11 import os
12 import sys
13 
14 ###########################################################
15 def openr (fname):
16 #
17 # Open file fname for reading
18 #
19 
20  try:
21  fl = open (fname,'r')
22  except IOError:
23  print " ! file.openr: Cannot open file '"+fname+"'"
24  sys.exit()
25 
26  return (fl)
27 
28 ###########################################################
29 def read (fname):
30 #
31 # Read a file fname and return a list
32 # containing all the lines.
33 #
34 
35  fl = openr(fname)
36  lines = fl.readlines()
37  fl.close()
38 
39  return (lines)
40 
41 ###########################################################
42 def create_file(f_name, lines):
43 
44 #
45 # Creat a file f_name,
46 # where lines is a list containing the lines
47 #
48 
49  fl = open(f_name,'w')
50  for tmp in lines: fl.write(tmp)
51  fl.close()
52 
53 
54 ##########################################################
55 def delete_lines(fname, lbeg, lend):
56 
57 #
58 # Delete lines from lbeg to lend in
59 # file fname; lbeg = 0is considered the
60 # first line in the file
61 #
62 
63  scrh = read (fname)
64  del scrh[lbeg:lend + 1]
65  create_file (fname, scrh)
66 
67 ##########################################################
68 def read_lines(fname, lbeg, lend):
69 
70 #
71 # Read lines from lbeg to lend in
72 # file fname; lbeg = 0 is considered the
73 # first line in the file
74 #
75 
76  scrh = read(fname)
77  lscrh = len(scrh)
78  if (lend > lscrh): lend = lscrh
79  return (scrh[lbeg:lend])
80 
81 ###########################################################
82 def insert (fname, string, lbeg):
83 
84 #
85 # Insert strings "string" at line "lbeg" in file
86 # fname.
87 #
88 
89  scrh = read(fname)
90  scrh[lbeg:lbeg] = [string]
91  create_file (fname, scrh);
92 
93 ###########################################################
94 def replace_line(fname, newstring, nline):
95 
96 #
97 # Replaces line number nline with newline
98 #
99 
100  scrh = read(fname)
101  del scrh[nline]
102  scrh[nline:1] = [newstring]
103 
104  create_file (fname, scrh);
105 
106 ###########################################################
107 def string_list(f_name, str):
108 
109 #
110 # Return a list of all the lines in file f_name
111 # where str occurs at least once as a SUBSTRING.
112 #
113 
114  return find(f_name,str,action='string',want='list')
115 
116 ###########################################################
117 def string_find(f_name, str):
118 
119 #
120 # Return a list of all the line numbers in file f_name
121 # where str occurs at least once as a SUBSTRING.
122 #
123 
124  return find(f_name,str,action='string',want='enum')
125 
126 ###########################################################
127 def word_list(f_name, str):
128 
129 #
130 # Return a list of all the lines in file f_name
131 # where str occurs at least once as a SEPARATE WORD.
132 #
133 
134 
135  return find(f_name, str,action='word',want='list')
136 
137 ###########################################################
138 def word_find(f_name, str):
139 
140 #
141 # Return a list of all the line numbers in file f_name
142 # where str occurs at least once as a SEPARATE WORD.
143 #
144 
145 
146  return find(f_name, str,action='word',want='enum')
147 
148 
149 ###########################################################
150 def find(fname, str, action, want):
151 
152  scrh = read(fname)
153 
154  line_str = []
155  line_num = []
156  ipos = -1
157  for tmp in scrh:
158  ipos = ipos + 1
159  x = string.split(tmp)
160  if (action == 'string'):
161 
162  if (string.find(tmp,str) >= 0):
163  line_str.append(tmp)
164  line_num.append(ipos)
165 
166  elif (action == 'word'):
167 
168  if (x.count(str) > 0):
169  line_str.append(tmp)
170  line_num.append(ipos)
171 
172 
173  if (want == 'list' ):
174  return line_str
175  elif (want == 'enum'):
176  return line_num
177 
178 ###########################################################
179 def count_lines(fname):
180 
181 #
182 # Return the number of lines in file f_name
183 #
184 
185  scrh = read(fname)
186  nlines = 0
187  for x in scrh:
188  if (len(string.split(x)) != 0):
189  nlines = nlines + 1
190 
191  return nlines
192 
193 ###########################################################
194 def count_words(f_name, i_line, f_line = -1):
195 
196 #
197 # Return a list with all the words
198 # from i_line (included) to f_line;
199 # if f_line is not given, end of
200 # file is assumed.
201 #
202 
203  try:
204  fl = open(f_name,'r')
205  except IOError:
206  print "Error in count_words [file.py]:"
207  print "Cannot open file '"+f_name+"'"
208  sys.exit()
209 
210 # go to line i_line
211 
212  tmp = []
213  for i in range(i_line): fl.readline()
214  if (f_line < 0):
215  tmp = fl.readlines()
216  else:
217  for i in range(f_line-i_line+1):
218  tmp.append(fl.readline())
219 
220 
221  scrh = []
222  for yy in tmp:
223  this_line = string.split(yy)
224  if (len(this_line) != 0):
225  for x in this_line:
226  scrh.append(x)
227 
228  return scrh
229 
230 
231 
232 
233 ##########################################################
234 def swap_lines(fname, l1, l2):
235 
236 #
237 # swap lines l1 and l2 in file fname
238 #
239 
240  scrh = read (fname)
241 
242  line_1 = scrh[l1]
243  line_2 = scrh[l2]
244 
245  replace_line (fname, l1, line_2)
246  replace_line (fname, l2, line_1)
def word_find(f_name, str)
Definition: file.py:138
def insert(fname, string, lbeg)
Definition: file.py:82
def replace_line(fname, newstring, nline)
Definition: file.py:94
def read(fname)
Definition: file.py:29
def count_lines(fname)
Definition: file.py:179
def delete_lines(fname, lbeg, lend)
Definition: file.py:55
def swap_lines(fname, l1, l2)
Definition: file.py:234
def openr(fname)
Definition: file.py:15
def find(fname, str, action, want)
Definition: file.py:150
def create_file(f_name, lines)
Definition: file.py:42
def count_words
Definition: file.py:194
def string_list(f_name, str)
Definition: file.py:107
def read_lines(fname, lbeg, lend)
Definition: file.py:68
def word_list(f_name, str)
Definition: file.py:127
def string_find(f_name, str)
Definition: file.py:117