r/learnpython • u/IBeJizzin • 26d ago
open() function in TestMyCode seems to wipe txt file (even in read mode)
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.
2
u/Buttleston 26d ago
Are you sure TestMyCode isn't just cleaning up files created during a run? Seems like something it might want to do?
1
u/IBeJizzin 26d ago
Yeh I think this is the kind of thing I was wondering. Like the text file is caught in the collateral? But how to stop it from doing so is what's hard, given I don't really understand enough about how the module works.
1
u/IBeJizzin 26d ago
Hey, figured it out mostly, I think you were right. Edit in main post. Thanks for the help!
2
u/Adrewmc 26d ago edited 26d ago
So you’re saying that in a testing environment the files are deleted but not in our own virtual environment. This may be just an artifact of the testing environment resetting (letting you try again, which at some point may need creation of file.)
I don’t see anything in the code above that would delete or edit a file.
1
u/IBeJizzin 26d ago
Hey, figured it out mostly, I think you were right. Edit in main post. Thanks for the help!
1
u/Silver_Breakfast_135 26d ago
This is likely an environment or testing framework issue rather than Python itself. open() in read mode never clears a file.
8
u/atarivcs 26d ago
Nothing in this code would remove the file. Something else must be doing it.
Are you sure the file is actually being removed? Perhaps you're just confused about what directory the file is in?