OSError: [Errno 9] Bad file descriptor

Hello Everyone

Iam working with Visual Studio Code and got the Error OSError: [Errno 9] Bad file descriptor.
When I run the file on my GitHub account, its running, but on my Laptop occurs the Error.

Do you have any clue for me?

Error Message:
Traceback (most recent call last):
File “d:\Markus\00_Documents\20_Bildung\60_Self_Online_Study\60_Programmieren\10_Python\100_programme_2.py”, line 1124, in
Workfile(r"D:\Markus\00_Documents\text3.txt")
File “d:\Markus\00_Documents\20_Bildung\60_Self_Online_Study\60_Programmieren\10_Python\100_programme_2.py”, line 1120, in Workfile
file2.close()
OSError: [Errno 9] Bad file descriptor

Program:

def Workfile(file):
    with open(file,"r") as file1:
        with open(r"D:\Markus\00_Documents\text4.txt","w") as file2:
            for row in file1:
                file2.write(row.rstrip("\n"))
            file2.close()
        file1.close()

Workfile(r"D:\Markus\00_Documents\text3.txt")

The point of the with statement is that you don’t need to explicitly close the file, so you’re effectively double-closing your files. You can save yourself the trouble :slight_smile:

Thanks a lot. But it didn´t worked without closing too.

Do you haver further clues?

The code you show oughtn’t be able to reproduce the problem by itself.

Here are some thing to look into: