0

I have several files in a directory and I need to have them displayed in one graph.

i have this code currently


import os
import matplotlib.pyplot as plt
import pandas as pd


directory = '/Users/jake/Desktop/New-Good'

for filename in os.listdir(directory):
    if filename.endswith(".txt") :
        data = pd.read_csv(filename, sep='\t', header=None, skiprows = "14", )
        # data = pd.DataFrame(data) # This step is redundant
        plt.plot(data[0], data[1], label = filename.split('.')[0])
    else:
        continue

plt.xlim(0.15)

plt.show()

the files all look like this


Data from leafazo_Absorbance_00001.txt Node

Date: Tue Nov 01 13:37:09 EDT 2022
User: LabUser
Spectrometer: USB4E02819
Trigger mode: 4
Integration Time (sec): 1.493900E-1
Scans to average: 1
Electric dark correction enabled: true
Nonlinearity correction enabled: false
Boxcar width: 0
XAxis mode: Wavelengths
Number of Pixels in Spectrum: 3648
>>>>>Begin Spectral Data<<<<<
178.15685   -0.17
178.37334   -0.17
178.58982   -0.17
178.80629   -0.54907
179.02276   -1.28573
179.23922   0.12028
179.45566   -0.32471
179.6721    -0.20396
179.88854   0.7574
180.10496   -0.88634
180.32137   -0.05073
180.53778   0.98356
180.75418   -0
180.97057   -0
181.18695   -0
181.40332   -0
181.61968   -0.32757
181.83604   0.74914
182.05238   -0.3794
182.26872   -0
182.48505   -0.88142
182.70137   -0.12168
182.91769   -0.04061
183.13399   -0.28814
183.35029   -0.08273
183.56657   0.00521
183.78285   0.16606
183.99912   -0.16458
184.21539   0.00916
184.43164   0.26657
184.64788   -0.4397
184.86412   -0.09054
185.08035   -0.06919
185.29657   -1.48624
185.51278   -0.3548
185.72898   -0.22575
185.94517   0.18213
186.16136   -0.03326
186.37753   -0.5215
186.5937    -0.40722

i recived this error

/Users/jake/PycharmProjects/pythonProject1/venv/bin/python /Users/jake/PycharmProjects/pythonProject1/main.py 
Traceback (most recent call last):
  File "/Users/jake/PycharmProjects/pythonProject1/main.py", line 11, in <module>
    data = pd.read_csv(filename, sep='\t', header=None, skiprows = "14", )
  File "/Users/jake/PycharmProjects/pythonProject1/venv/lib/python3.10/site-packages/pandas/util/_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "/Users/jake/PycharmProjects/pythonProject1/venv/lib/python3.10/site-packages/pandas/util/_decorators.py", line 331, in wrapper
    return func(*args, **kwargs)
  File "/Users/jake/PycharmProjects/pythonProject1/venv/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 950, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "/Users/jake/PycharmProjects/pythonProject1/venv/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 605, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "/Users/jake/PycharmProjects/pythonProject1/venv/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 1442, in __init__
    self._engine = self._make_engine(f, self.engine)
  File "/Users/jake/PycharmProjects/pythonProject1/venv/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 1735, in _make_engine
    self.handles = get_handle(
  File "/Users/jake/PycharmProjects/pythonProject1/venv/lib/python3.10/site-packages/pandas/io/common.py", line 856, in get_handle
    handle = open(
FileNotFoundError: [Errno 2] No such file or directory: 'leafazo_Absorbance_00072.txt'

Process finished with exit code 1

Any help would be greatly appreciated. Im trying to take 70 different files from a Spectrometer.if anyone has any other directio to point me in, that would be appreciated

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • As the error says, `No such file or directory: 'leafazo_Absorbance_00072.txt'`. Clearly this file is not present. The file name is `leafazo_Absorbance_00001.txt` – taurus05 Nov 28 '22 at 06:09
  • this s not the issue as the program doesn't expect this file. I have several files – Jacob Bracken Nov 28 '22 at 06:54
  • Well, apparently `'leafazo_Absorbance_00072.txt'` doesn't exist in the current directory. Providing the full path could work better: `pd.read_csv(directory+'/'+filename, ...)`. To make it less dependent on the operating system, use `os.path.abspath(os.path.join(directory, filename))`. – JohanC Nov 28 '22 at 08:56
  • do i need to list all 72 files in my code? – Jacob Bracken Nov 28 '22 at 13:06

0 Answers0