Created
December 26, 2015 08:14
-
-
Save jackdalton/8a27ba1a35fab521811a to your computer and use it in GitHub Desktop.
Update GitHub repo links in a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[jack@arch]$ ls -R testdir | |
testdir: | |
a.txt testdir2 | |
testdir/testdir2: | |
b.txt | |
[jack@arch]$ cat testdir/a.txt | |
def | |
[jack@arch]$ cat testdir/testdir2/b.txt | |
def123 | |
[jack@arch]$ python replacelinks.py | |
Target directory? : testdir | |
Current text? : def | |
Desired text? : abc | |
Wrote to testdir/a.txt . | |
Wrote to testdir/testdir2/b.txt . | |
Target string replaced 2 times. | |
[jack@arch]$ cat testdir/a.txt | |
abc | |
[jack@arch]$ cat testdir/testdir2/b.txt | |
abc123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import zipfile, os, re | |
def init(): | |
dirname = input("Target directory? : ") | |
from_str = input("Current text? : ") | |
to_str = input("Desired text? : ") | |
reps = 0 | |
for sd, ds, fs in os.walk(dirname): | |
for file in fs: | |
fp = sd + os.sep + file | |
fr = open(fp, "r") | |
fcont = fr.read() | |
fr.close() | |
fw = open(fp, "w") | |
fw.write(fcont.replace(from_str, to_str)) | |
fw.close() | |
fr = open(fp, "r") | |
fcont2 = fr.read() | |
fr.close() | |
if fcont != fcont2: | |
reps += 1 | |
print("Wrote to", fp, ".\n") | |
else: | |
print("Target string not found in", fp, ".\n") | |
print("Target string replaced", reps, "times.\n") | |
init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment