文件系统迭代 [英] file system iteration

查看:88
本文介绍了文件系统迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Unix中,文件系统层次结构就像一棵树,它有一个基础或

''root'',它可以很容易地暴露对象(文件和文件夹)。 $ b迭代过来。

\ \ | / /

\ \ | / /

\ \ | / /

\ | /

\ | /

|

|

Root


所以,当我做os.chdir(''/'')时我就在树的基础上,现在可以使用

这样的东西像os.walk()来处理所有的文件系统对象。


在Windows中,文件系统是脱节的,现在有真正的根'

至少没有我能看到的。它看起来更像是:


| | | | | | |

| _ | _ | _ | _ | _ | _ |

ABCDEFG


你们怎么处理这个使用需要触摸的脚本

Windows机器上的所有文件和文件夹?我一直在循环浏览

AZ这样:


import os.path


paths = [ ]


如果是os.path.isdir(''A:/''):

paths.append(''A:/'')<如果是os.path.isdir(''B:/''),那么


paths.append(''B:/'')


....


这是一个kludge,但它运作正常。我确定WMI可能有一个函数,即
返回已安装的卷,但在目前的情况下,我只能使用标准的Python库。关于如何做得更好的任何想法?


谢谢

In Unix, the file system hierarchy is like a tree that has a base or
''root'' that exposes objects (files and folders) that can easily be
iterated over.
\ \ | / /
\ \ | / /
\ \|/ /
\ | /
\|/
|
|
Root

So, when I do os.chdir(''/'') I am at the base of the tree and can now use
something like os.walk() to work with all of the file system objects.

In Windows, the file system is disjointed and there is now real ''root''
At least none that I can see. It looks more like this:

| | | | | | |
|_|_|_|_|_|_|
A B C D E F G

How do you guys handle this when working with scripts that need to touch
all files and folders on a Windows machine? I''ve been looping through
A-Z like this:

import os.path

paths = []

if os.path.isdir(''A:/''):
paths.append(''A:/'')

if os.path.isdir(''B:/''):
paths.append(''B:/'')

....

That''s a kludge, but it works OK. I''m sure WMI may have a function that
returns mounted volumes, but under the circumstances currently, I can
only use the standard Python library. Any ideas on how to do this better?

Thanks

推荐答案

2006- 10-09 14:45:35 + 0200,rick写道:
On 2006-10-09 14:45:35 +0200, rick wrote:

import os.path


paths = []


如果os.path.isdir(''A:/''):

paths.append(''A:/'')

paths.append(''B:/'')


...


这是一个很好的例子,但它运作正常。我确定WMI可能有一个函数,即
返回已安装的卷,但在目前的情况下,我只能使用标准的Python库。关于如何做得更好的任何想法?
import os.path

paths = []

if os.path.isdir(''A:/''):
paths.append(''A:/'')

if os.path.isdir(''B:/''):
paths.append(''B:/'')

...

That''s a kludge, but it works OK. I''m sure WMI may have a function that
returns mounted volumes, but under the circumstances currently, I can
only use the standard Python library. Any ideas on how to do this better?



至少你可以尝试:


导入字符串

string.ascii_uppercase

如果是os.path.isdir(''%s:/''%c):

...




但我想应该有更好的方法。


Gerrit 。

The very least you can try:

import string
string.ascii_uppercase

for c in string.ascii_uppercase:
if os.path.isdir(''%s:/'' % c):
...

etc.
But I suppose there should be a better way.

Gerrit.


Gerrit Holl写道:
Gerrit Holl wrote:

至少你可以尝试:

导入字符串

string.ascii_uppercase


for c in string.ascii_uppercase:

if os.path。 isdir(''%s:/''%c):

...




但我想应该有更好的方法。
The very least you can try:

import string
string.ascii_uppercase

for c in string.ascii_uppercase:
if os.path.isdir(''%s:/'' % c):
...

etc.
But I suppose there should be a better way.



哦,是的,我这样做。我非常明确地阐述了这个例子,因为

清晰度。我实际上并没有输入AZ :)

Oh yes, I do that. I spelled out the example very explicitly for
clarity. I don''t actually type in A-Z :)


rick写道:
rick wrote:

在Unix中,文件系统层次结构就像一棵树,它有一个基础或

''root'',它可以显示对象(文件和文件夹),这些对象很容易被迭代过来。
>

\ \ | / /

\ \ | / /

\ \ | / /

\ | /

\ | /

|

|

Root


所以,当我做os.chdir(''/'')时我就在树的基础上,现在可以使用

这样的东西像os.walk()来处理所有的文件系统对象。


在Windows中,文件系统是脱节的,现在有真正的根'

至少没有我能看到的。它看起来更像是:


| | | | | | |

| _ | _ | _ | _ | _ | _ |

ABCDEFG


你们怎么处理这个使用需要触摸的脚本

Windows机器上的所有文件和文件夹?我一直在循环浏览

A-Z像这样:
In Unix, the file system hierarchy is like a tree that has a base or
''root'' that exposes objects (files and folders) that can easily be
iterated over.
\ \ | / /
\ \ | / /
\ \|/ /
\ | /
\|/
|
|
Root

So, when I do os.chdir(''/'') I am at the base of the tree and can now use
something like os.walk() to work with all of the file system objects.

In Windows, the file system is disjointed and there is now real ''root''
At least none that I can see. It looks more like this:

| | | | | | |
|_|_|_|_|_|_|
A B C D E F G

How do you guys handle this when working with scripts that need to touch
all files and folders on a Windows machine? I''ve been looping through
A-Z like this:



哪个应用程序需要遍历所有文件?通常情况下,你只需要一个

的起始路径并遍历其下的所有内容。


在Unix中,事情也不是那么清楚。例如,有一些符号链接

使树更复杂。或者不同的文件系统安装在

不同的挂载点上,甚至可能甚至不代表像

/ proc文件系统这样的真实文件。在迭代所有文件时需要谨慎。


Georg

Which application needs to walk over ALL files? Normally, you just have a
starting path and walk over everything under it.

In Unix, things aren''t so clear either. For example, there are symbolic links
that make the tree more complicated. Or different file system mounted on
different mount points, perhaps not even representing real files like the
/proc filesystem. All that needs caution when iterating over "all files".

Georg


这篇关于文件系统迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆