【问题标题】:Opening a file in python gives the error IOError: [Errno 2] No such file or directory: '' on MAc在 python 中打开文件会出现错误 IOError: [Errno 2] No such file or directory: '' on MAc
【发布时间】:2018-09-03 11:56:06
【问题描述】:

我经常为我的任务运行代码,但这是我第一次在 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

代码是-

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)

能否请您告诉我在 Mac 上遇到了什么问题以及如何解决?

【问题讨论】:

标签: python macos python-2.7


【解决方案1】:

我刚刚在我的 iMac 上尝试了您的文件,但它没有运行完成。它给出了第 58 行的错误消息,即语句 print device。 Python 3 及更高版本要求打印语句带有括号,因此您的行应该是print (device)。您运行它的另一台计算机可能使用了 python 2,而您的 Mac 可能使用的是 python3。我在 print 语句中添加了括号,您的代码运行完成。所以也许你想修复打印语句,看看是否有帮助。

我在 VScode 和终端中运行它。它在两种情况下都运行完成,没有错误消息。据我所知,根本没有输出。我想这个脚本在我没有的另一个文件上运行。所以我的运行完成可能不可靠。但是,我没有收到任何错误消息,所以这可能会告诉你一些事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 2012-08-25
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多