I'm attempting to access a .txt file while accessing a module function I made. Problem is I don't think the function can access the text file. The file has data in it. It goes through the motions of trying to populate it. Do I need a init.py? I'm getting now output in my data.csv file that I'm attempting to output. But my data.txt file has data in it. So I'm not sure what I'm doing wrong. When run the file on it's on not as a module it runs perfectly.
The call:
import csv_generator
#choose to print out in csv or json
def printData():
answer = input('Please choose 1 for csv or 2 for jsson output file: ')
if(answer == '1'):
csv_generator.csv_write()
elif(answer == '2'):
json_writer.json_write()
else:
printData()
printData()
The definition:
import csv
def csv_write():
#files to read from
data_read = open('data.txt', 'r')
#process data and write to csv file
with open('data.csv', 'w', newline='') as f:
thewriter = csv.writer(f)
thewriter.writerow(['Last', 'First', 'Email', 'Address', 'Phone'])
for x in data_read:
y = x.split(',')
thewriter.writerow([y[0].strip(), y[1].strip(), y[2].strip(), y[3].strip(), y[4].strip()])