python - os.walk 在 Mac 和 Linux 上排序不同的文件夹?

标签 python linux macos python-3.5 os.walk

给定以下文件结构,

├── 0=ocfl_object_1.0
├── inventory.json
├── inventory.json.md5
├── v1
│   ├── content
│   │   ├── foo.xml
│   │   └── level1
│   │       └── level2
│   │           └── bar.txt
│   ├── inventory.json
│   └── inventory.json.md5
└── v2
    ├── content
    │   └── duck.txt
    ├── inventory.json
    └── inventory.json.md5

我想知道 python 的 os.walk 函数是否有可能在 Mac 和 Linux 上以不同的顺序返回文件夹?两者都使用 python 3.5。

麦克:

In [15]: for root,folders,files in os.walk('foo/bar'): 
    ...:     print(folders,files) 
    ...:                                                                                                                                                                                                                                                                                   
['v1', 'v2'] ['inventory.json', '0=ocfl_object_1.0', 'inventory.json.md5']
['content'] ['inventory.json', 'inventory.json.md5']
['level1'] ['foo.xml']
['level2'] []
[] ['bar.txt']
['content'] ['inventory.json', 'inventory.json.md5']
[] ['duck.txt']

在 Linux 上:

In [54]: for root,folders,files in os.walk('foo/bar'): 
    ...:     print(folders,files) 
    ...:                                                                                                                                                                                                                                                                                   
['v2', 'v1'] ['inventory.json.md5', 'inventory.json', '0=ocfl_object_1.0']
['content'] ['inventory.json.md5', 'inventory.json']
[] ['duck.txt']
['content'] ['inventory.json.md5', 'inventory.json']
['level1'] ['foo.xml']
['level2'] []
[] ['bar.txt']

在 Mac 的情况下,看起来好像首先遇到文件夹 v1,而在 Linux 上它是 v2。关于为什么会出现这种情况的任何见解?

最佳答案

参见 documentation on os.walk , 相关部分:

Changed in version 3.5: This function now calls os.scandir() instead of os.listdir(), making it faster by reducing the number of calls to os.stat().

然后在os.scandir() :

Return an iterator of os.DirEntry objects corresponding to the entries in the directory given by path. The entries are yielded in arbitrary order, and the special entries '.' and '..' are not included.

无论是 listdir() 还是 scandir(),都以任意顺序返回。

简而言之 - 订单是不可预料的。


话虽如此,你应该能够基于这部分在循环中操作dirnames:

When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is False has no effect on the behavior of the walk, because in bottom-up mode the directories in dirnames are generated before dirpath itself is generated.

因此,如果您folders.sort() 它应该根据您的sorted 顺序工作。我刚刚试过了,它起作用了。我还加粗了关键部分 in-place - folders 必须就地排序以便 os.walk() 接受订单:

for root,folders,files in os.walk('foo/bar'): 
    folders.sort()   # <--- sort your folders to impose the order. 
    print(folders,files) 

关于python - os.walk 在 Mac 和 Linux 上排序不同的文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54773598/

相关文章:

c++ - 使用 Qt 时,我是否必须在特定操作系统上才能针对特定操作系统进行编译?

macos - Bash:在 OS X 下的字符串中查找字符的位置

eclipse - Eclipse 中项目验证时出错 - clean

Python round() 太慢,更快的方法来降低精度?

c - 我们如何丢弃目标文件中的符号并在查看核心转储时重新使用它?

node.js - 使用 Alpine 部署 Azure Function - 未处理的 'error' 事件

c - init 收割孤儿进程后,shell 等待输入

Python-如何向字符串添加空白字符以便向数据库添加列

javascript - Python - Flask - Google Charts API 获取每个 CategoryFilter 中的值并将其传递给 href

python - Django forms.VaildationError 未显示在基于类的 View 上