2016-01-14 94 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」與從站點到文件名的映射)。

相關問題