It’s in the title, I don’t know if it is a replit problem or python problem but I’m asking it anyways
The code to make this problem is like this
In a package, or a function, make this code(I got the problem in package but I’m not sure so):
with open('OhNo.txt', 'r') as f:
_=input('data: ' + (data := f.read()))
if data == 'it failed bruh ':
In main.py write this
from package import oof #if the code above is in package
with open('OhNo.txt', 'w') as file:
file.write('it failed bruh')
as result you should get 2 output
normally you should get
data: it failed bruh
but as result I somehow get
it reads a file that is written ‘it failed bruh’, and what it read is not ‘it failed bruh’ but ‘’ (an empty string)
I have no idea why but it annoys me so much
Apart form the fact the code is really weird, you are opening the file while it is already open. The effect of this depends a lot on the OS you are running this on.
Also when doing this, you need to keep an eye on where the filer cursor is. In general it is always better to close the file before opening it again.
So… is there any way to fix it, only changing the package code?
All I need is either fix this issue or detect this issue to report back
try to move the print outside of the with block. this will close the file before opening it
The code is weird is because it is to demonstrate the problem only. I am making a package and can only change the package code, as I don’t know what the user will do
So is there any way to detect if user opened the file, or fix the problem, with ONLY changing package/function code?
I answered the question above. Try to move the print out of the with loop.
See i this solves the issue as doing so will close the file after writing and should reset the cursor position on any OS.
That is the code on the client side, which is the user’s code. My goal is to either find this problem within the function and call the user to fix it, or able to access the file
The goal is to keep the code in main.py, not changing it, if possible.
Ok, I am trying to understand f this is the problem .
Let’s try to reset the cursor, add f.seek(0,0) after with open (in the with block).
If this does not help, then the error is something else.
May 4, 2023, 6:16am
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.