Why is there no output showing in this python code?
<pre><code>man=[]

other=[]

try:
    data=open('sketch.txt')
    for each_line in data:
        try:
            (role,line_spoken) = each_line.split(':',1)
            line_spoken= line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()

except IOError:
    print('The datafile is missing!')
try:
    man_file=open('man_data.txt','w')
    other_file=open('other_data.txt','w')
    print(man, file=man_file)
    print(other, file=other_file)
    man_file.close()
    other_file.close()
except IOError:
    print('File error.')
</code></pre>

<p>Shouldn't it create man_data and other_data file?
There is no error message or any kind of input in the idle.</p>

<p><a href="https://i.stack.imgur.com/V0pD3.png" rel="nofollow"><img src="https://i.stack.imgur.com/V0pD3.png" alt="enter image description here"></a></p>

<p>The indentation in your screenshot is different from your question. In your question you claimed your code was this (with some bits trimmed out):</p>

<pre><code>try:
    # Do something
except IOError:
    # Handle error
try:
    # Write to man_data.txt and other_data.txt
except IOError:
    # Handle error
</code></pre>

<p>But your screenshot shows that you actually ran this code:</p>

<pre><code>try:
    # Do something
except IOError:
    # Handle error
    try:
        # Write to man_data.txt and other_data.txt
    except IOError:
        # Handle error
</code></pre>

<p>The whole of the second <code>try</code>/<code>except</code> block is within the <code>except</code> clause of the first one, so it will only be executed if there is an exception in the first <code>try</code> block. The solution is to run the code that is in your question i.e. unindent the second <code>try</code>/<code>except</code> block so that it is at the same level as the first one.</p>

python python-3.x