0

I'm new in python and i'm strugling with some problem... I'm trying to get absolute path for each files with extenction .cls or .js and after that I'm trying to read them and check if some pattern existing in reading file but i got error that

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\MyUser\\Desktop\\scripts\\TEST_FILE.cls'

with double slashs

import csv
import xml.etree.ElementTree as ET
import xml.dom.minidom as PT
import traceback
import glob, os
import traceback
from pathlib import Path

CLASSES_EXT = '.cls'
JS_EXT = '.js'

CLASSES_PATH = "../src/classes/"
LWC_PATH = "../src/lwc/"

#Function to get list of specific files
#argument
#path - is path where we are looking for files
#extention - extention that we are looking for
def getAbsolutePathForFiles(path, extention):
    try:
        filesList = os.listdir(path)
        retVal = []
        for file in filesList:
            if(file.endswith(extention)):
                retVal.append(os.path.abspath(file))
    except Exception:
        traceback.print_exc()
    return retVal

# function to check if label is using somwhere
def checkIfFileContainsLabel(fileName, label):
    try:
        filePath = Path(fileName);
        print('CO TU ', filePath)
        openedFile = open(Path(fileName), 'r+', encoding='utf-8')
        #with open(filePath , 'r') as readFile:
        for line in readFile:
            if label in line:
                return True
        return False
    except Exception:
        traceback.print_exc()



#Get list of file in which we will be looking for labels
classesFileList = getAbsolutePathForFiles(CLASSES_PATH, CLASSES_EXT)
jsFileList = getAbsolutePathForFiles(LWC_PATH, JS_EXT)

for pathToFile in classesFileList:
    retVal = checkIfFileContainsLabel(pathToFile,"CPT_From")
    print('RETVALU ', retVal)
artist
  • 319
  • 1
  • 5
  • 18
  • The double slashes are just Python's way to disambiguate them. It should be easy to verify that the actual string only contains individual backslashes as directory separators. – tripleee Oct 17 '20 at 07:49
  • when i do print(fileName) in chekIfFileContainsLabel it show me string with one backsleshes – artist Oct 17 '20 at 08:01
  • Exactly. See the duplicate. – tripleee Oct 17 '20 at 08:07
  • so how can i easly fix it ? – artist Oct 17 '20 at 08:09
  • If the computer says the file doesn't exist, believe it. Common beginner mistakes are misunderstanding which directory the file is actually in (try dragging the file to a console / terminal / cmd window to reveal its full path; maybe see also [Difference between `./` and `~/`](https://stackoverflow.com/a/55342466/874188)) and simply mistyping the path. – tripleee Oct 17 '20 at 08:20
  • Like the now deleted commenter, I also wonder why you think you need to convert the file names to absolute paths; but this has no bearing on (what appears to be) your actual question. – tripleee Oct 17 '20 at 08:38

0 Answers0