Tzahi Kadosh Tzahi Kadosh - 1 month ago 9
Python Question

error 2 No such file or directory

i'm trying to run python script which already ran on test environment.
already checked if the path correct and if the file in it.
checked in shell that the file exist.

current code is :

# Open a file
path = 'C:\\Users\\tzahi.k\\Desktop\\netzer\\'
dirs = os.listdir( path )
fileslst = []
alertsCode = (some data)


# loop over to search the relative file
for file in dirs:
if "ALERTS" in file.upper() :
fileslst.append(file)
fileslst.sort()

#open and modify the latest file
with open(fileslst[-1], 'rb') as csvfile:
csvReader = csv.reader(csvfile)
clean_rows = [row for row in csvReader if not any(alert in row[2] for alert in alertsCode)]


error :

IOError:error 2 no such file or directory:'file name'


when i debug in shell i see the path and files

what am i doing wrong?

Answer

os.listdir() lists the files relative to the directory.

You need to add the full directory path to the filename for it to be an absolute path again:

with open(os.path.join(path, fileslst[-1]), 'rb') as csvfile: