-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyGlobSplitter.py
60 lines (48 loc) · 1.46 KB
/
pyGlobSplitter.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
"""
File Splitter
splits a single python file in to several python files
for a file in the form:
###Hello, World!###
print "Hello, World!"
##
###Goodbye to the world Types###
...
code
...
##
###Next Code Segment###
...
"""
import os, re, sys
# replace all function to help with the clean up
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
def filecreator(a,b):
symbols = {",":".","!":"", " ":"","\n":""}
filename = "%s.py" % replace_all(a,symbols)
writefile = open(writelocation + filename, 'w')
print("Filename: %s " % filename)
writefile.write(b)
print("File contents: \n%s" % b)
# current directory
currDir = os.path()
# read in file
origfile = open("pythontidbits.txt")
writelocation = "\\output\\"
# read in contents
origcontents = origfile.read()
# setup re so it can easily split the lists into file then file names from the files
myre = re.compile("\n##\n")
filenamere = re.compile("###\n")
# split the file original files into lists based on
# ##\n which is the indicator of end of a file
origFileList = myre.split(origcontents)
filesChomped = [ origFile.replace("###","",1) for origFile in origFileList]
# a list of lists ...
# this has a list of [ file name, file contents] lists
files = [ filenamere.split(indvfile) for indvfile in filesChomped]
# use a list comprehension to iterate through the list of files and create the files
[filecreator(x[0],x[1]) for x in files]