在python中打开文件会出现错误IOError:[Errno 2]没有这样的文件或目录:'';在MAc上
我经常为我的任务运行代码,但这是我第一次在Mac上运行它,它给了我错误-在python中打开文件会出现错误IOError:[Errno 2]没有这样的文件或目录:'';在MAc上,python,macos,python-2.7,Python,Macos,Python 2.7,我经常为我的任务运行代码,但这是我第一次在Mac上运行它,它给了我错误- Traceback (most recent call last): File "bt.py", line 103, in <module> with open(filename,"a") as fout: NameError: name 'filename' is not defined 你能告诉我Mac有什么问题,我该如何解决吗?我刚刚在iMac
Traceback (most recent call last):
File "bt.py", line 103, in <module>
with open(filename,"a") as fout:
NameError: name 'filename' is not defined
你能告诉我Mac有什么问题,我该如何解决吗?我刚刚在iMac上尝试了你的文件,但没有完成。它给出了第58行的错误消息,该行是语句
打印设备
。Python3及以上版本要求print语句带有括号,因此您的行应该是print(device)
。运行它的另一台计算机可能使用了python 2,而Mac可能使用了python 3。我在print语句中添加了括号,您的代码运行完毕。所以,也许你想修正print语句,看看这是否有帮助
我用VScode运行它,也从终端运行。在这两种情况下,它都运行到完成,没有错误消息。据我所知,根本没有输出。我想这个脚本运行在另一个我没有的文件上。因此,我的跑步完成可能是不可靠的。但是,我没有收到任何错误消息,因此这可能会告诉您一些事情。您的标题与Traceback不一致。而且这似乎不一致,我建议您从
import time
from datetime import datetime, timedelta
import re
import zipfile
import glob, os
import string
str=""
new_name=""
new_name1=""
date_folder_name=""
flag=0
file_name=""
file_name2=""
filename=""
line2=""
lines=""
i=0
path=""
clock=""
device=""
command=""
commandName=""
data=""
#..\input\BT
inputBTFolder=os.path.join('..','input','BT')
#Extracting Zip File and Storing Date
for root, dirs, files in os.walk(inputBTFolder):
for file in files:
if '.zip' in file:
print("Processing:"+file)
file_name=file.split('.zip')[0]
file_name2=file_name.split('_')
zip=zipfile.ZipFile(os.path.join(inputBTFolder,file))
zip.extractall(os.path.join(inputBTFolder,file.split('.zip')[0]))
year='20'+file_name2[6]
month=file_name2[5]
date=file_name2[4]
Date=date+"-"+month+"-"+year
dateformat=datetime.strptime(Date,'%d-%m-%Y')
clock=dateformat.strftime("%Y%m%d")+"000000"
for root, dirs, files in os.walk(os.path.join(inputBTFolder,file_name)):
for file in files:
f=open(os.path.join(root,file), "r")
for line in f:
crInfo='CR INFO'
#i=0
if 'Connecting to ' in line :
deviceName=line.split(" ")
device=deviceName[2]
print device
path=os.path.join('..','output','British-Telecom','cli',clock,device)
if os.path.exists(path):
continue
else :
os.makedirs(path)
#Code for each device starts from here -
if 'executing' in line:
commandName=line.split("'")
command=commandName[1]
if ("/" or "*" or ":" or "%" or "-" or "|" or " " in command) :
if("|" in command):
commandname1=command.split("|")
command=commandname1[0]
command=command.replace("/", "_")
command=command.replace("*", "_")
command=command.replace(":", "_")
command=command.replace("%", "_")
command=command.replace("-", "_")
command=command.replace(" ", "_")
if("__" in command):
command=command.replace("__", "_")
if(len(command)>0 and command[-1]=='_'):
command=command[:-1]
filename=path+"/"+command+".txt"
open(filename,'a').close()
if crInfo not in line:
with open(filename,"a") as fout:
fout.write(line)