coding windows-1251 import os random sys stat time re replacement need

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: windows-1251 -*-
import os, random, sys, stat, time, re
replacement = ""
needle = ""
def ProcessFile(DIR, FILES):
for file2del in FILES:
# если папка - обойдём рекурсивно
if os.path.isdir(DIR + file2del) and (DIR + file2del != ".") and (DIR + file2del != ".."):
print "Dir found: ", DIR + file2del
dirList = os.listdir(DIR + file2del)
if (DIR + file2del)[len(DIR + file2del) - 1] != "\\":
ProcessFile(DIR + file2del + "\\", dirList)
else:
ProcessFile(DIR + file2del, dirList)
if os.path.isfile(DIR + file2del):
print '-'*80, '\nprocessing file: %s\n%s' % (file2del, '-'*80)
hfile = open(DIR + file2del, 'r')
lines = hfile.read()
hfile.close()
hfile = open(DIR + file2del, 'wb')
lines.replace(needle, replacement)
hfile.close()
start = time.time()
try:
print 'Starting search for needed text...\n\n'
ProcessFile("", [sys.argv[1]])
print "%.2f seconds total" % (time.time() - start)
except IndexError:
print "Usage: filesreplace.py <filename or folder name>"