-
Notifications
You must be signed in to change notification settings - Fork 1
/
FTDReader.py
136 lines (99 loc) · 4.22 KB
/
FTDReader.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#Copyright Zachary Kolansky, 2021.
#Use as you like
import os,shutil
from distutils.util import strtobool
'''
The purpose of this program is to assit in analyzing FTD for a specific ticker from the SEC
Data: https://www.sec.gov/data/foiadocsfailsdatahtm
It will read N number of FTD files,
look for a specific ticker symbol, get that file line and remember it.
Once we read all the files in the data folder, we write all the lines we
remembered to one output file.
Additionally, I substitute all '|' for ','. Helps with csv making
'''
'''
returns the ticker,inputFolder, outputFolder, and override
from a given configuration file
override: Whether to override the output folder.
'''
def GetConfigurationSettings(configFileName = "config.txt"):
config = open(configFileName,'r')
configAsList = config.readlines() #convert config file to a list
configString = configAsList[1] #has config data in the second line, 0 = first line
configSplit = configString.split(',')
ticker = configSplit[0].strip() #removal \n, other whitespace
inputFolder = configSplit[1].strip()
outputFolder = configSplit[2].strip()
override = strtobool(configSplit[3].strip().lower())
config.close()
return ticker,inputFolder,outputFolder,override
'''
If override is true, we delete the contents of a given directory, then make a new directory of the same name
if override is false, we will make a new directory, with name equal Directory + N, where N is number of directories in the current working directory
that have the given Directory name. This means if there is already a folder called Output, we will make a folder called Output2 and so on
returns the name of directory this function creates.
'''
def MakeNewDirectory(Directory,override = True):
FinalDirectoryName = Directory
if(override):
if(os.path.exists(Directory)):
shutil.rmtree(Directory)
os.mkdir(Directory)
else:
os.mkdir(Directory)
else: #make new seperate folder
if(os.path.exists(Directory)):
cwdFiles = os.listdir()
numberOfSimiliarlyNamedDir = int(str(cwdFiles).count(Directory) + 1) #number of output folders
NewDirName = Directory + str(numberOfSimiliarlyNamedDir) #create valid output file folder
os.mkdir(NewDirName)
FinalDirectoryName = NewDirName
else:
os.mkdir(Directory)
return FinalDirectoryName
#MAIN
ticker,inputFolder,outputFolder,override = GetConfigurationSettings()
outputFolder = MakeNewDirectory(outputFolder,override)
if(os.path.isdir(inputFolder)):
os.chdir(inputFolder)
else:
raise FileNotFoundError("InputFolder for data " + inputFolder + " not found! Can't continue!")
dataFileList = os.listdir() # get all the text files in the folder.
listOfRelevantTickerLines = []
fileHeader = ""
bDoOnce = True
#print(dataFileList)
for dataFileName in dataFileList:
dataFile = open(dataFileName,'r',encoding='latin1')
data = dataFile.readlines()
for line in data:
lineAsList = line.split('|')
if(len(lineAsList) < 3):
break #prevent index out of bounds error, likely from EOL
if(lineAsList[2] != ticker):
continue
else:
lineReplaced = line.replace('|',',') # want csv file
#listOfRelevantTickerLines.append(dataFileName + ',' + lineReplaced.strip())
listOfRelevantTickerLines.append(lineReplaced.strip())
dataFile.close()
if(bDoOnce): #get the header to write to the outfile, assume all input files are formatted the same
dataFile = open(dataFileName,'r')
fileHeaderRaw = dataFile.readline().strip()
fileHeader = fileHeaderRaw.replace('|',',') # want csv file
dataFile.close()
bDoOnce = False
os.chdir('../')
os.chdir(outputFolder)
#print(os.getcwd())
outputFile = open("output.txt",'w')
outputFile.write(fileHeader + '\n')
for line in listOfRelevantTickerLines:
outputFile.write(line + '\n')
os.chdir('../')
outputFile.close() #flush the text to write to the actual file
print("-------------------------\n")
print("Finished for Ticker " + ticker + " !" + '\n')
print("-------------------------\n")
if(ticker == "GME"):
print("Power to the players.\n")