使用os.path.normpath时遇到Windows路径名问题

时间:2016-03-05 20:21:27

标签: windows backslash

 if msg.startswith("!links "):
        msg = msg[7:]
        self.msg(channel, "These are all the recent links entered by " + msg)

        t = msg + '.txt'
        t = os.path.join(r"log/", t)
        print t
        f = open(os.path.normpath(t), 'r')
        f.read()

这段代码给了我这个错误:    exceptions.IOError:[Errno 2]没有这样的文件或目录:' log \\ SapphireLightning.txt' 注意神奇的额外反斜杠?

日志/应该已经转到日志\而不是从我读过的有关如何在python中使用Windows目录的所有内容中记录\\。我试图找到另一种处理反斜杠的方法,但无济于事......我错过了什么?

编辑:我从" log"中删除了反斜杠。字符串,现在我收到此错误:

C:\bot>ircLogBot.py #FIMOtherkin test.txt
2016-03-05 15:43:09-0500 [-] Log opened.
2016-03-05 15:43:09-0500 [-] Starting factory <__main__.LogBotFactory instance at 0x0225CFD0>
2016-03-05 15:43:09-0500 [-] [connected at Sat Mar 05 15:43:09 2016]
2016-03-05 15:43:10-0500 [-] [I have joined #FIMOtherkin]
2016-03-05 15:43:20-0500 [-] <SapphireLightning> !links SapphireLightning
2016-03-05 15:43:20-0500 [-] log\SapphireLightning.txt
2016-03-05 15:43:20-0500 [LogBot,client] Unhandled Error
        Traceback (most recent call last):
          File "C:\Python27\lib\site-packages\twisted\internet\tcp.py", line 215, in _dataReceived
            rval = self.protocol.dataReceived(data)
          File "C:\Python27\lib\site-packages\twisted\words\protocols\irc.py", line 2622, in dataReceived
            basic.LineReceiver.dataReceived(self, data.replace('\r', ''))
          File "C:\Python27\lib\site-packages\twisted\protocols\basic.py", line 571, in dataReceived
            why = self.lineReceived(line)
          File "C:\Python27\lib\site-packages\twisted\words\protocols\irc.py", line 2630, in lineReceived
            self.handleCommand(command, prefix, params)
        --- <exception caught here> ---
          File "C:\Python27\lib\site-packages\twisted\words\protocols\irc.py", line 2685, in handleCommand
            method(prefix, params)
          File "C:\Python27\lib\site-packages\twisted\words\protocols\irc.py", line 2050, in irc_PRIVMSG
            self.privmsg(user, channel, message)
          File "C:\bot\ircLogBot.py", line 212, in privmsg
            f = open(os.path.normpath(t), 'r')
        exceptions.IOError: [Errno 2] No such file or     
    directory: 'log\\SapphireLightning.txt'

请注意,它可以从打印命令(打印t)正确打印,但是当它传递给打开时会破坏...

0 个答案:

没有答案