开发者

os.walk and read file, having some problems with walking the path

开发者 https://www.devze.com 2023-04-01 07:21 出处:网络
Here is the entirety of my code, I am trying to debug it, but it seems like I can\'t do any reading of files while using os.walk.The problem is that without changing directories, I am hit with a error

Here is the entirety of my code, I am trying to debug it, but it seems like I can't do any reading of files while using os.walk. The problem is that without changing directories, I am hit with a error that 'out.csv' cannot be found, so i put in a chdir(), to move to that directory to read that file, but now it will only read that one f开发者_Go百科ile. My estimate is there should be over 300+ of these files. So it seems to stop after just the first file read after I put in the chdir().

#! /usr/bin/env python
import csv, os

current = os.curdir
filescntd = 0 
avg = 0 

for root, dirs, files in os.walk('ARCIVE'):
  for file in files:
    base, ext = os.path.splitext(file)
    if ('csv' in ext):
      os.chdir(root)
      print root
      f = csv.reader(open('out.csv','rb'))
      count = 0 
      for row in f:
        if (count >= 6 and count <= 10):
          tempavg = 0 
          for i in row:
            tempavg += float(i)
            filescntd += 1
          tempavg /= len(row)
          avg += tempavg
        count += 1
      os.chdir(current)

os.chdir(current)
print '---'
avg /= 5.0 
print avg 

output:

ARCIVE/8-15-11/temp/29033
---
0.02775

the option of filescntd is a little misleading, it is the amount of numbers averaged, and it comes to 40.

To clarify, what I want this program to do, is walk this directory tree and open all files that contain 'csv' in the extension, and read lines 6-10, and average those lines. I am having problems with the walking the path and opening the files.


Remove chdir and do f = csv.reader(open(os.path.join(root, 'out.csv'),'rb'))

0

精彩评论

暂无评论...
验证码 换一张
取 消