python上的IOError 22在Windows上无效 [英] IOError 22 in python Invalid on windows

查看:93
本文介绍了python上的IOError 22在Windows上无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python创建用于串行端口的嗅探器,但是在Windows中创建CSV文件时遇到了问题.我在某些方面拆分了程序,以避免Windows和linux之间不兼容的可能性.它在Linux上完美运行(在32和64字节上测试).

I'm creating a sniffer for serial port in python, but I have a problem when i create a CSV file in windows. I split my program on some point to avoid the possibility of incompatibility between windows and linux. It's works perfectly on linux (test on 32 and 64 bytes).

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"])

创建文件夹Sniffer_Serie_Result时没有错误 因此,此代码首先使用以下代码返回以下错误:

The folder Sniffer_Serie_Result is create without error So this code return the following error at the first with:

IOError:[Errno 22]无效的参数:'C:\ Documents and Settings \ stagiaire \ Sniffer_Serie_Result \ Request_at _......(实际日期和时间).csv'

IOError: [Errno 22] Invalid argument: 'C:\Documents and Settings\stagiaire\Sniffer_Serie_Result\Request_at_......(Actual date and hours).csv'

我尝试了很多诸如原始字符串之类的字符串,但无济于事.

I try lot of string like raw string and nothing works.

NB:我用于测试的Windows是XP,这也需要在7上运行

NB: The windows i use for my test is XP, this need to work on 7 too

希望您能帮助我. 谢谢你的帮助!

I hope you can help me. Tks for your help!

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

I can't give no more information before thursday (no internet at home for the moment)

推荐答案

您正在尝试在文件名中使用:字符,而Windows中保留了该字符作为驱动器名称(例如c:/).

You are trying to use : characters in filename, while that character is reserved in Windows for drive names (e.g. c:/).

您必须:

  1. 修改timestampWithoutMilli()以使用另一个时间分隔符(例如-)
  2. 例如,将获得的时间字符串中的所有:替换为另一个字符(使用.replace()).
  1. Modify timestampWithoutMilli() to use another time separator (like -),
  2. Substitute all : in obtained time string with another character (using .replace()), for example.

这篇关于python上的IOError 22在Windows上无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆