gpt4 book ai didi

Python 错误 os.walk IOError

转载 作者:太空宇宙 更新时间:2023-11-03 18:19:02 25 4
gpt4 key购买 nike

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

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 is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do 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);

关于Python 错误 os.walk IOError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24493065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com