0

Though files are in same directory I'm getting with open(file, 'r') as f: FileNotFoundError: [Errno 2] No such file or directory: 'breast_cancer.txt'

enter image description here

I've following code under pre_process_data.py:

import re

cancer_text = ['breast_cancer.txt', 'colorectal_cancer.txt', 'lung_cancer.txt', 'prostate_cancer.txt',
               'skin_cancer.txt']

for file in cancer_text:
    # print(file)
    with open(file, 'r') as f:
        text = f.read()
        f.close()
    with open(file, 'w') as f:
        print('pre-processing the file {}'.format(file))
        text = re.sub("[!@#$+%*:()'-]", ' ', text)
        text = text.lower()
        f.write(text)
        f.close()

The files are in same directory so this should be working but I've no idea why this is happening

Amrutha k
  • 39
  • 1
  • 4
  • Perhaps this post answers your question? https://stackoverflow.com/questions/4060221/how-to-reliably-open-a-file-in-the-same-directory-as-the-currently-running-scrip – OuterSoda Apr 20 '23 at 11:41
  • can you post the full stack trace? i tried it locally and it went through till the print statement. Also make sure to run the script within the folder where your files are available or else you may have to give the absolute/relative path – Kulasangar Apr 20 '23 at 11:42

0 Answers0