#πŸ”’ Bad File Descriptor problem in Python

42 messages Β· Page 1 of 1 (latest)

lean lava
#

I am trying to write to a text file in Python. The name of the file is testfile.txt. Here is what I tried:

f = open('testfile.txt', 'r')

print(f.read())
Hello out there.

f.close()
f = open('testfile.txt', 'a')
f.writable()
True
f.write('Right back at ya!')
17
f.close()
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 9] Bad file descriptor

Even thought it looks like it is writing to the file, it isn't. What am I doing wrong? Thanks in advance.

stable widgetBOT
#

@lean lava

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tepid marlin
#

def backward(self, dy):
# Backward pass through the RNN
batch_size, seq_len, output_dim = dy.shape
dWy = np.zeros_like(self.Wy)
dby = np.zeros_like(self.by)
dWh = np.zeros_like(self.Wh)
dbh = np.zeros_like(self.bh)
dWx = np.zeros_like(self.Wx)

    dh_next = np.zeros_like(self.h)
    
    for t in reversed(range(seq_len)):
        dy_t = dy[:, t, :]
        dWy += np.dot(self.h_stack[:, t, :].T, dy_t)
        dby += np.sum(dy_t, axis=0, keepdims=True)
        
        dh_t = np.dot(dy_t, self.Wy.T) + dh_next
        dh_tanh = (1 - self.h_stack[:, t, :] ** 2) * dh_t
        
        dWx += np.dot(self.x[:, t, :].T, dh_tanh)
        dbh += np.sum(dh_tanh, axis=0, keepdims=True)
        dWh += np.dot(self.h_prev.T, dh_tanh)
        
        dh_next = np.dot(dh_tanh, self.Wh.T)

    This is the backpropagation for rnn. In weight adjusting we use dot product but for bias we use summation, please explain this
tender steppe
tender steppe
lean lava
lean lava
tender steppe
#

sorry, i was responding to the other person who hijacked your post πŸ˜…

lean lava
tender steppe
#

Does the program work if you run it from a file (eg main.py)? I am not 100% sure why you have this issue, but it might be exclusive to the Python REPL (command prompt) because the f deconstructor could have been called somehow before you called f.close. pithink

lean lava
prisma ridge
#

Remember to use code blocks

#

!code

stable widgetBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

lean lava
#

Here is all the code there is in the file:
with open('testfile.txt', 'w') as f:
f.write("right back at ya")
There is no more to it than that. Here is message:
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "c:\Users\rwalt\Documents\pcode\hello\writing.py", line 1, in <module>
with open('testfile.txt', 'w') as f:
OSError: [Errno 9] Bad file descriptor

It seems like this in my previous message I just the version where I interacted directly with the command prompt.

lean lava
lean lava
prisma ridge
#

So I suspect there's more output you're not showing

lean lava
prisma ridge
#

There should be more before that

#

Remember to use code blocks

lean lava
#

Really, there is no more. I copied it right off the screen.

lean lava
prisma ridge
#

Can you send a screenshot?

lean lava
#

OK. Hold on. How do I send the screen shot on this. I have never used this before.

lean lava
lean lava
lean lava
prisma ridge
#

There's bizarre

lean lava
lean lava
# prisma ridge There's bizarre

And here is the screen shot of the same interaction from the command prompt. I even checked to see if the file was writable and it appears to be so.

cloud tartan
#

@lean lava When you open a file using with open, the end of the with closes the file for you. When you go f.close() after that, the file is already closed. The OS file descriptor associated with the file is no longer valid, since it isn't associated witrh an open file.

tender steppe
tender steppe
lean lava
lean lava
stable widgetBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.