os.步行Mac和Linux上的文件夹顺序不同?

2024-05-14 23:27:19 发布

您现在位置:Python中文网/ 问答频道 /正文

给定以下文件结构

├── 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上以不同的顺序返回文件夹?两者都使用python3.5。你知道吗

Mac地址:

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。你知道为什么会这样吗?你知道吗


Tags: txtjsonobjectfoobarfilesxmlcontent
1条回答
网友
1楼 · 发布于 2024-05-14 23:27:19

参见documentation on ^{},相关部分:

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().

然后在^{}

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顺序工作。我刚试过,效果不错。我还用粗体标出了关键部分,即folders必须按顺序进行排序os.walk()

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

相关问题 更多 >

    热门问题