Python – File does not exist error


I'm trying to do a couple things here with the script below (it is incomplete). The first thing is to loop through some subdirectories. I was able to do that successfully. The second thing was to open a specific file (it is the same name in each subdirectory) and find the minimum and maximum value in each column EXCEPT the first.

Right now I'm stuck on finding the max value in a single column because the files I'm reading have two rows which I want to ignore. Unfortunately, I'm getting the following error when attempting to run the code:

Traceback (most recent call last):
  File "", line 22, in <module>
    with open(file) as f:
IOError: [Errno 2] No such file or directory: 'tc.out'

Here is the current state of my code:

import scipy as sp
import os

rootdir = 'mydir'; #mydir has been changed from the actual directory path
data = []

for root, dirs, files in os.walk(rootdir):
    for file in files:
        if file == "tc.out":
            with open(file) as f:
                for line in itertools.islice(f,3,None):
                    for line in file:
                    fields = line.split()
                    rowdata = map(float, fields)
                    print 'Maximum: ', max(data)

Best Solution

To open a file you need to specify full path. You need to change the line

with open(file) as f:


with open(os.path.join(root, file)) as f: