0

I'm trying to filter my experiment measurements using a Fourier filter, which works. Now I got multiple raw data files (.txt) in a folder which I would like to filter and then place in a different folder.

The structure is as follows;

Filter script: C:\Users\myname\Desktop\folder1\Scripts

Raw data folder: C:\Users\myname\Desktop\folder1\Scripts\Raw_data

Filtered data folder: C:\Users\myname\Desktop\folder1\Scripts\Filtered_data

My code is:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy.fftpack import rfft, irfft, fftfreq

data = pd.read_csv(r'C:\Users\myname\Desktop\folder1\Scripts\Raw_data\rawtest_datafile1.txt')

time   = np.linspace(0, 4017, 4018)
signal = data.TG

print(Filter(time, signal, 0.004,  0.0075, False, 0.04)/10)

The result in a 1D-array.

Now I would like to know how I can import all files rawtest_datafile[i].txt, filter them, and make a new file called filteredtest_datafile[i].txt one by one using a loop.

I hope this is clear enough.

Ruben
  • 3
  • 3
  • Possible duplicate of [How can I iterate over files in a given directory?](https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory) – BcK May 16 '18 at 10:14

2 Answers2

2

Use the following sample code , and modify as per needs.

data_file_format="C:\Users\myname\Desktop\\folder1\Scripts\Raw_data\\rawtest_datafile{}.txt";

output_file_format="C:\Users\Ruben\Desktop\BEP\Scripts\Filtered_data\output_file{}.txt";

for i in range(1,10):
    datafile=data_file_format.format(i);
    outputfile=output_file_format.format(i);
    data = pd.read_csv(datafile);
    time   = time = np.linspace(0, 4017, 4018)
    signal = data.TG
    print_to_file(Filter(time, signal, 0.004,  0.0075, False, 0.04)/10,outputfile);

Note: 1. print_to_file is a function that you need to define. it should take two inputs, the data you need to print and filepath.

  1. You need to know the number of data files to run the for loop. otherwise you should use os module.
0

You can use os.listdir() in Python's os module to get the contents of a directory. Following this, check if its a file using using isfile from the same module. Perform filter operation if it is a file and write the file to the destination.

import os

for content in os.listdir(source_path):
    if os.path.isfile(os.path.join(source_path, content)):
        # read file and perform filter operation
        new_file = "filtered" + content
        # write data to os.path.join(dest_path, new_file)

Hope this helps.

Ritik Saxena
  • 694
  • 1
  • 11
  • 23