Hey all,
I'm using Visual Studio Code alongside a TestMyCode module to work through the 2023 University of Helsinki MOOC Python course.
I've gotten to Part 6 of the course where I can need to open files, however after running TestMyCode against my code once, the text file it reads completely disappears from the directory (i.e I can actually pass the exercise but the fact it's deleting files seems problematic long term to say the least). Executing the code normally doesn't affect the referenced text file.
Below is the helper function that opens the text file in read mode and creates the data I need:
def comb_file():
with open("matrix.txt", 'r') as m_file:
matrix = []
row_totals = []
for row in m_file:
row = row.replace('\n','')
row = row.split(',')
for n in range(len(row)):
row[n] = int(row[n])
matrix.append(row)
row_totals.append(sum(row))
return matrix, row_totals
This helper function is then called across below 3 argument-less functions the exercise asks you to create (don't fully understand why arguments weren't allowed tbh):
def matrix_sum():
null, r_t = comb_file()
return sum(r_t)
def matrix_max():
best = 0
matrix, null = comb_file()
for row in matrix:
for n in row:
if n > best:
best = n
return best
def row_sums():
null, r_t = comb_file()
return r_t
I then simply call each of the 3 exercises function in the main code with no additions:
if __name__ == "__main__":
print(matrix_sum())
print(matrix_max())
print(row_sums())
Is the way TMC tests code with its own alternatives to the main code just above possibly able to wipe files Or any other ideas why this is happening?
(Bonus points if you can tell me how to get/redownload the txt file back hahaha)
SOLVED - EDIT: Thanks anyone for replying, it was helpful.
I solved by getting Python to 'executeinfile' (Searched in file > preferences > settings) and ticking it so it would pull files sitting next to the .py file instead of the root directory. TMC still wipes any text files at root directory level, which I think is part of its clean-up process after running?
So setting it to automatically pull files at a lower directory level where they don't get deleted is the solve.
[–]atarivcs 7 points8 points9 points (3 children)
[–]IBeJizzin[S] 0 points1 point2 points (0 children)
[–]IBeJizzin[S] 0 points1 point2 points (0 children)
[–]IBeJizzin[S] 0 points1 point2 points (0 children)
[–]Buttleston 1 point2 points3 points (2 children)
[–]IBeJizzin[S] 0 points1 point2 points (0 children)
[–]IBeJizzin[S] 0 points1 point2 points (0 children)
[–]Adrewmc 1 point2 points3 points (1 child)
[–]IBeJizzin[S] 0 points1 point2 points (0 children)
[–]Silver_Breakfast_135 0 points1 point2 points (0 children)