I wrote a simple function to read a file line by line, do some calculation and store the results in another file. However, when I output
# Record cumulative battery per second
freq = 1000 # number of samples per second
output_file = os.path.splitext(input_file) + "_1s" + ".txt"
output = open(output_file, 'a+')
count = 0
for line2 in fileinput.input(input_file):
count = count + 1
if count == freq:
count = 0
I don't know the specifics of your application, but I would send the data to a list, and then use Python's slice notation to select every 1000th data point.
Try the following snippet.
data = range(100) every_tenth = data[::10]
Also, you can eliminate the need to explicitly close files if you use the
with open(filename) as f: data = f.readlines()
The following is your code re-written more 'Pythonically'--that is, it takes advantage of some of the shortcuts that Python offers you over C. You may have to fiddle with it a little to get it to run. I am not sure how you are handling your files.
def CumulativePerSecond(filename, freq=1000): data =  with open(filename) as input_file: data = input_file.readlines() output_file = os.path.splitext(filename) + "_1s.txt" with open(out_filename, 'a+') as output: for line in data[::freq]: output.write(line)