1

\Anaconda3\lib\tempfile.py", line 483, in func_wrapper return func(*args, **kwargs) ValueError: read of closed file

I don't know if this is the issue even I can't figure this out. This is being called in a loop and the first loops works fine but errors on the second loop. Reading the file it says

"This class provides a wrapper around files opened for temporary use. In particular, it seeks to automatically remove the file when it is no longer needed."

But it is still needed and closing anyway, causing the error. This is my best guess.

I'm using this because a normal request is blocked by the server. Which by the way, a normal request loops and works fine (tested unblocked links). This one works on the first cycle then fails on the second. I think because the "link" variable is being reused.

  class AppURLopener(urllib.request.FancyURLopener):
      version = "Mozilla/5.0"

looping part

  opener = AppURLopener()
  linkSource = opener.open(link).read() #error on this line "read of closed file (second run through)"

I tried to modify the code so that the "link" variable is different which I'm guessing would solve it but the only way I could find to do that was make it a list or a dictionary. Both of those don't work because the base variable is still closed I guess.

In that case I tried using open() at the start of the loop which produces an error like this "TypeError: expected str, bytes or os.PathLike object, not dict"

If I just have it be a normal string it doesn't work either which on another point doesn't make sense because above it says "expected str", I assumed if it was just a normal str it would work but it just produces another error "OSError: [Errno 22] Invalid argument: http://www.link.com"

I guess random variables can't be used with open() like that. So why is it closed? Or is something else closed?

I don't know.

  • I changed "opener" to a dictionary and created a new one for each loop and I'm getting the exact same error. I really thought that would work. – cakeeater9001 Jul 24 '18 at 01:26
  • The website seems to be somehow blocking the second request. Which makes no sense, why would the first one always work but the second one always gets block. I can run this program over and over again and the first one always works and the second one is blocked. If i change the website it loops fine. I don't understand. – cakeeater9001 Jul 24 '18 at 02:18

1 Answers1

0

The second url I was using had a "#!" at the end of it. Somehow that broke it in the program. Parsed it out and works fine.