本文介绍了Python错误os.walk IOError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用文件名中的服务器跟踪文件,并且可以使用服务器**来打印目录中的所有文件,但是当我尝试读取文件时,它给了我错误提示:

I tried to track the file with server in the filename and i can print all the file in directory with server** but when I try to read the file it gives me error" saying:

Traceback (most recent call last):
  File "view_log_packetloss.sh", line 27, in <module>
    with open(filename,'rb') as files:
IOError: [Errno 2] No such file or directory: 'pcoip_server_2014_05_19_00000560.txt'

我已经看到类似的问题,但我无法解决我的问题,使用chdir将当前目录更改为文件目录已修复了一些错误.任何帮助表示赞赏.谢谢

I have seen similar question being asked but I could not fix mine, some error were fixed using chdir to change the current directory to the file directory. Any help is appreciated. Thank you

#!usr/bin/env/ python
import sys, re, os

#fucntion to find the packetloss data in pcoip server files
def function_pcoip_packetloss(filename):
        lineContains = re.compile('.*Loss=.*')  #look for "Loss=" in the file
        for line in filename:
                if lineContains.match(line):    #check if line matches "Loss="
                        print 'The file has: '  #prints if "Loss=" is found
                        print line
                        return 0;

for root, dirs, files in os.walk("/users/home10/tshrestha/brb-view/logs/vdm-sdct-agent/pcoip-logs"):
        lineContainsServerFile = re.compile('.*server.*')
        for filename in files:
                if lineContainsServerFile.match(filename):
                        with open(filename,'rb') as files:
                                print 'filename'
                                function_pcoip_packetloss(filename);

推荐答案

文件是根目录中文件对象的名称.

the files are names of file objects in root directory.

尝试

for root, dirs, files in os.walk("/users/home10/tshrestha/brb-view/logs/vdm-sdct-agent/pcoip-logs"):
    lineContainsServerFile = re.compile('.*server.*')
    for filename in files:
            if lineContainsServerFile.match(filename):
                    filename = os.path.join(root, filename)
                    with open(filename,'rb') as files:
                            print 'filename:', filename
                            function_pcoip_packetloss(filename);

这篇关于Python错误os.walk IOError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 04:47