我试图用文件名中的服务器跟踪文件,并且可以使用服务器**来打印目录中的所有文件,但是当我尝试读取文件时,它给了我错误提示:
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将当前目录更改为文件目录已修复了一些错误。任何帮助表示赞赏。谢谢
#!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);
这些文件是根目录中文件对象的名称。
dirpath是一个字符串,是目录的路径。dirnames是dirpath中子目录名称的列表(不包括“。”和“ ..”)。filenames是dirpath中非目录文件名称的列表。请注意,列表中的名称不包含路径成分。要获取目录路径中文件或目录的完整路径(从顶部开始),请执行os.path.join(dirpath,name)。
尝试这个
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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句