简体   繁体   English

如何在python中扫描目录?

[英]How can I scan through a directory in python?

I have a python script that is trying to compare two files to each other and output the difference. 我有一个python脚本试图将两个文件相互比较并输出差异。 However I am not sure what exactly is going on as when I run the script it gives me an error as 但是我不确定运行脚本时到底发生了什么,它给我一个错误

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

I dont know why it is appending * . 我不知道为什么要加上*。 * at the end of the file extention. *在文件扩展名末尾。

This is currently my function: 这是我目前的职能:

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)

Any thoughts on why it cant scan the directory and look for an os file for example? 关于为什么它无法扫描目录并寻找os文件的任何想法?

It is failing on line: 它在网上失败了:

for os_filename in os.listdir(latest_filename):

The code first gets called from 该代码首先被调用

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)

Thank you 谢谢

os.listdir() requires that the latest_path argument be a directory as you have stated. os.listdir()要求last_path参数是您所述的目录。 However, latest_path is being passed in as an argument. 但是,latest_path是作为参数传递的。 Thus, you need to look at the code that actually creates latest_path in order to determine why the ' . 因此,您需要查看实际创建latest_path的代码,以确定为什么使用' ' is being put in. Since you are calling it recursively, first check the original call (the first time). 正在插入'。由于您是递归调用的,因此请首先检查原始调用(第一次)。 It would appear that your base code that calls CheckFilesLatest() is trying to set up the search command to find all files within the directory 'C:\\api\\API_TEST\\Apis.os' You would need to split out the file indicator first and then do the check. 您的调用CheckFilesLatest()的基本代码似乎正在尝试设置搜索命令以查找目录'C:\\ api \\ API_TEST \\ Apis.os'中的所有文件。您需要首先拆分文件指示符,然后然后做检查。

If you want to browse a directory recursively, using os.walk would be better and simpler than your complex handling with recursive function calls. 如果要递归浏览目录,使用os.walk会比使用递归函数调用进行复杂处理更好,更简单。 Take a look at the docs: http://docs.python.org/2/library/os.html#os.walk 看一下文档: http : //docs.python.org/2/library/os.html#os.walk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM