Question: When I am trying to run this script I am getting this error:

> 
> ValueError: I/O operation on closed file.
> 
> 
> 

I checked some similar questions and the doc, yet no success. And while the error is clear enough I haven't been able to figure it out. Apparently I am missing something.

```
# -*- coding: utf-8 -*-
import os
import re

dirpath = 'path\\to\\dir'
filenames = os.listdir(dirpath)
nb = 0

open('path\\to\\dir\\file.txt', 'w') as outfile:
    for fname in filenames:
        nb = nb+1
        print fname
        print nb
        currentfile = os.path.join(dirpath, fname)

open(currentfile) as infile:
    for line in infile:
        outfile.write(line)

```

**Edit:** Since i removed the `with` from `open` the message error changed to: 

```
`open (C:\\path\\to\\\\file.txt, 'w') as outfile` :

```

> 
> SyntaxError : invalid syntax with a pointer underneath as
> 
> 
> 

**Edit:** Much confusion with this question. After all, i restored `with` and i fixed the indents a bit. And it is working just fine!

Answer: 
Since they pages appear on different origins: 

1. The page containing the frame needs to [listen](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener) for a Message event.
2. The page inside the frame needs to send a message using [`postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage).

This, obviously, requires changes on both sites. Explicit co-operation between the sites is required for obvious security reasons (if they aren't obvious, imagine your bank's website being loaded in an iframe by a random site you visited via Google).
Check this link: <http://simple.procoding.net/2008/03/21/how-to-access-iframe-in-jquery/>

```
$('#iframeID').contents().find('#someID').html();

```