简体   繁体   English

在python3中使用os.walk时文件读取不正确

[英]Incorrect file reading when using os.walk in python3

I am crawling through folders using the os.walk() method. 我正在使用os.walk()方法浏览文件夹。 In one of the folders, there is a large number of files, around 100,000 of them. 在其中一个文件夹中,有大量文件,其中约有100,000个。 The files look like: p_123_456.zip . 文件如下: p_123_456.zip But they are read as p123456.zip . 但是它们被读取为p123456.zip Indeed, when I open windows explorer to browse the folder, for the first several seconds the files look like p123456.zip , but then change their appearance to p_123_456.zip . 确实,当我打开Windows资源管理器浏览文件夹时,最初的几秒钟文件看起来像是p123456.zip ,但是随后将其外观更改为p_123_456.zip This is a strange scenario. 这是一个奇怪的情况。

Now, I can't use time.sleep() because all folders and and files are being read into python variables in the looping line. 现在,我不能使用time.sleep()因为所有文件夹和文件都在循环行中被读取到python变量中。 Here is a snippet of the code: 这是代码片段:

    for root, dirs, files in os.walk(srcFolder):
        os.chdir(root)
        for file in files:
            shutil.copy(file, storeFolder)

In the last line, I get a file not found exception, saying that the file p123456.zip does not exist. 在最后一行,我得到一个文件未找到异常,说文件p123456.zip不存在。 Has anyone run into this mysterious issue? 有谁遇到这个神秘的问题? Anyway to bypass this? 反正绕过这个? What is the cause of this? 这是什么原因? Thank you. 谢谢。

You don't seem to be concatenating the actual folder name with the filenames. 您似乎并没有将实际的文件夹名称与文件名连接在一起。 Try changing your code to: 尝试将代码更改为:

for root, dirs, files in os.walk(srcFolder):
    for file in files:
        shutil.copy(os.path.join(root, file), storeFolder)

os.chdir should be avoided like the plague. os.chdir应该像瘟疫一样避免。 For one thing - if the changes suceeeds, it won't be the directory from which you are running your os.walk anymore - and then, a second chdir on another folder will fail (either stop your porgram or change you to an unexpected folder). 一方面-如果更改成功,它将不再是您运行os.walk的目录-然后,另一个文件夹上的第二个chdir将失败(停止porgram或将您更改为意外的文件夹)。

Just add the folder name as prefixes, and don't try using chdir. 只需将文件夹名称添加为前缀,不要尝试使用chdir。

Moreover, as for the comment from ShadowRanger above, os.walk officially breaks if you chdir inside its iteration - https://docs.python.org/3/library/os.html#os.walk - that is likely the root of the problem you had. 此外,作为从上述ShadowRanger的评论, os.walk正式如果你打破chdir内的迭代- https://docs.python.org/3/library/os.html#os.walk -这是有可能的根源你遇到的问题。

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

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