2016-01-14 83 views
1

的错误是这样的:无法写入[错误22]无效MOD或文件名

mini: mkrule www.google.com therule 
Traceback (most recent call last): 
    File "C:/Users/t11/Dropbox/2.0/cmd2.0.py", line 88, in <module> 
    MiniDownloader().cmdloop() 
    File "C:\Python27\lib\cmd.py", line 142, in cmdloop 
    stop = self.onecmd(line) 
    File "C:\Python27\lib\cmd.py", line 221, in onecmd 
    return func(arg) 
    File "C:/Users/t11/Dropbox/2.0/cmd2.0.py", line 75, in do_mkrule 
    functions2.write(self.RulesDir+site, xpath) 
    File "C:\Users\t11\Dropbox\2.0\functions2.py", line 12, in write 
    with open(path_file, mod) as FileObject: 
IOError: [Errno 22] invalid mode ('w') or filename: 'C:\\Users\\t11\\Dropbox\\2.0\\Rules\\https://www.google.com' 

Process finished with exit code 1 

我使用CMD模块 ,这是功能:

def write(path_file, data, mod="w", writeover=False): 
     """ gets file path + name and contents 
     check if the name is taken and if not write it (return's True if successful and False if not) """ 
     if os.path.exists(path_file) and writeover is not True: 
      return False 
     else: 
      with open(path_file, mod) as FileObject: 
       FileObject.write(data) 
       FileObject.flush() 
       return True 

我打电话这里的功能:

def do_mkrule(self, s): 
     s = s.split() 
     if len(s) != 2: 
      self.help_mkrule() 
      return 
     site = s[0] 
     xpath = s[1] 
     site = functions2.url_check(site) 
     if xpath == "del": 
      os.remove(self.RulesDir+site) 
      print "file "+site+" removed" 
     else: 
      if site != False: 
       functions2.write(self.RulesDir+site, xpath) 
       print "rule for "+site+" created" 

该函数的意图是获取文件路径,名称和内容,检查名称是采取,如果不写。

它应该返回True,如果成功则返回False,否则返回False。

回答

0

错误消息说它:

IOError: [Errno 22] invalid mode ('w') or filename: 'C:\\Users\\t11\\Dropbox\\2.0\\Rules\\https://www.google.com' 

你传递给函数的文件名是无效的。 Windows不接受文件名中的许多字符,包括冒号(:)和正斜杠(/)。

你可以摆脱它们(一个好的竞争者是用下划线替换无效字符),或者找到另一种在文件名中编码站点名称的方法,例如通过列出名称的索引(例如命名文件“sites.json”与从站点到文件名的映射)。

相关问题