python中的IOError 22在windows上无效

2024-10-01 17:33:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python为串行端口创建一个嗅探器,但是在windows中创建CSV文件时遇到了一个问题。为了避免windows和linux之间不兼容的可能性,我在某一点上拆分了我的程序。它在linux上运行得非常好(在32和64字节上进行测试)。在

def createNewFiles(self):
    # Nons allons vérifier l'existance du dossier Sniffer_Serie_Result et le créer si besoin
    # De même pour le fichier csv
    if (os.name == "nt"): # pour windows
        self.userPath = os.getenv('HOME') or os.getenv('USERPROFILE')
        self.folderPath= os.path.abspath(self.userPath + "\\Sniffer_Serie_Result")
        #exist_ok=True ==> cree le dossier si il n'existe pas
        os.makedirs(self.folderPath,exist_ok=True)
        self.timestampWithSec= self.timestampWithoutMilli() # utilisé dans les noms de fichier
        self.filePathRequest= os.path.abspath(self.folderPath + "\\Request_at_" + self.timestampWithSec + ".csv")
        self.filePathResponse= os.path.abspath(self.folderPath + "\\Response_at_" + self.timestampWithSec + ".csv")
        self.filePathOverall = os.path.abspath(self.folderPath + "\\Overall_result_at_" + self.timestampWithSec + ".csv")
        with open(self.filePathRequest, 'w') as f:
            writer = csv.writer(f)
            writer.writerow(["Kind of message","Timestamp","Message Hexa","Message ASCII"]) 
        with open(self.filePathResponse, 'w') as f:
            writer = csv.writer(f)
            writer.writerow(["Kind of message","Timestamp","Message Hexa","Message ASCII"])

创建文件夹嗅探器序列结果没有错误 因此,此代码首先返回以下错误:

IOError:[Errno 22]无效参数:“C:\Documents and Settings\stagiaire\Sniffer_Serie_Result\Request_at_….(实际日期和时间).csv”

我尝试了很多像原始弦一样的弦,但没有任何效果。在

注意:我的测试用的是XP,这也需要在7上运行

我希望你能帮助我。 谢谢你的帮助!在

我不能在星期四之前提供更多信息(家里暂时没有互联网)


Tags: csvpathselflemessageoswindowsresult
3条回答

您可能在userPath中找不到转义符。尝试将所有\更改为/。在

def createNewFiles(self):
        # Nons allons vérifier l'existance du dossier Sniffer_Serie_Result et le créer si besoin
        # De même pour le fichier csv
        if (os.name == "nt"): # pour windows
            self.userPath = os.getenv('HOME') or os.getenv('USERPROFILE')
            self.folderPath= self.userPath + "/Sniffer_Serie_Result"
            #exist_ok=True ==> cree le dossier si il n'existe pas
            os.makedirs(self.folderPath,exist_ok=True)
            self.timestampWithSec= self.timestampWithoutMilli() # utilisé dans les noms de fichier
            self.filePathRequest= self.folderPath + "/Request_at_" + self.timestampWithSec + ".csv"
            self.filePathResponse= self.folderPath + "/Response_at_" + self.timestampWithSec + ".csv"
            self.filePathOverall = self.folderPath + "/Overall_result_at_" + self.timestampWithSec + ".csv"
            with open(self.filePathRequest, 'w') as f:
                writer = csv.writer(f)
                writer.writerow(["Kind of message","Timestamp","Message Hexa","Message ASCII"]) 
            with open(self.filePathResponse, 'w') as f:
                writer = csv.writer(f)
                writer.writerow(["Kind of message","Timestamp","Message Hexa","Message ASCII"])

有了这段代码,问题就完全一样了,文件夹是创建的,而不是文件。同样的错误。在

您试图在文件名中使用:字符,而这个字符在Windows中是为驱动器名保留的(例如c:/)。在

你必须:

  1. 修改timestampWithoutMilli()以使用另一个时间分隔符(如-
  2. 例如,将获得的时间字符串中的所有:替换为另一个字符(例如使用.replace())。在

相关问题 更多 >

    热门问题