2014-03-05 55 views
0

我有一个python脚本试图将两个文件相互比较并输出差异。但我不知道究竟是怎么回事,当我运行它给我一个错误的脚本如何通过python中的目录进行扫描?

NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\api\\API_TEST\\Apis.os\\*.*' 

,因为我不知道为什么它是追加*。 *在文件扩展结束时。

这是目前我的功能:

def CheckFilesLatest(self, previous_path, latest_path): 

    for filename in os.listdir(latest_path): 

     previous_filename = os.path.join(previous_path, filename) 
     latest_filename = os.path.join(latest_path, filename) 

     if self.IsValidOspace(latest_filename): 

      for os_filename in os.listdir(latest_filename): 
       name, ext = os.path.splitext(os_filename) 

       if ext == ".os": 
        previous_os_filename = os.path.join(previous_filename, os_filename) 
        latest_os_filename = os.path.join(latest_filename, os_filename) 

        if os.path.isfile(latest_os_filename) == True: 

         # If the file exists in both directories, check if the files are different; otherwise mark the contents of the latest file as added. 
         if os.path.isfile(previous_os_filename) == True: 
          self.GetFeaturesModified(previous_os_filename, latest_os_filename) 
         else: 
          self.GetFeaturesAdded(latest_os_filename) 

     else: 
      if os.path.isdir(latest_filename): 
       self.CheckFilesLatest(previous_filename, latest_filename) 

为什么它不能扫描目录,并寻找适合例如OS文件有什么想法?

它失败在线:

for os_filename in os.listdir(latest_filename): 

代码首先会从

def main(): 
    for i in range(6, arg_length, 2): 
     component = sys.argv[i] 
     package = sys.argv[i+1] 

     previous_source_dir = os.path.join(previous_path, component, package) 
     latest_source_dir = os.path.join(latest_path, component, package) 

     x.CheckFilesLatest(previous_source_dir, latest_source_dir) 
     x.CheckFilesPrevious(previous_source_dir, latest_source_dir) 

称为谢谢

+1

如果你告诉我们在什么情况下引发异常,这将有所帮助。 –

+0

对不起,它的失败说明:对于os.listdir(latest_filename)中的os_filename: – user974873

+0

你能检查这个函数的argumetns是什么。在函数入口点添加打印语句 –

回答

1

os.listdir()要求latest_path参数是一个目录,就像你所说的那样。但是,latest_path被作为参数传入。因此,您需要查看实际创建latest_path的代码,以确定为什么''正被放入。由于您正在递归调用,请首先检查原始呼叫(第一次)。看起来您调用CheckFilesLatest()的基本代码尝试设置搜索命令来查找目录'C:\ api \ API_TEST \ Apis.os'中的所有文件。您需要首先拆分文件指示符,然后然后做检查。