Python列表目录、子目录和文件

时间:2022-01-21 21:41:26

I'm trying to make a script to list all directory, subdirectory, and files in a given directory.
I tried this:

我正在尝试编写一个脚本,列出给定目录中的所有目录、子目录和文件。我试着这样的:

import sys,os

root = "/home/patate/directory/"
path = os.path.join(root, "targetdirectory")

for r,d,f in os.walk(path):
    for file in f:
        print os.path.join(root,file)

Unfortunatly it doesn't work properly.
I get all the files, but not their complete paths.

不幸的是它不能正常工作。我得到所有的文件,但不是它们的完整路径。

For example if the dir struct would be:

例如,如果目录结构是:

/home/patate/directory/targetdirectory/123/456/789/file.txt

It would print:

它会打印:

/home/patate/directory/targetdirectory/file.txt

What I need is the first result. Any help would be greatly appreciated! Thanks.

我需要的是第一个结果。非常感谢您的帮助!谢谢。

6 个解决方案

#1


126  

Use os.path.join to concatenate the directory and file name:

使用os.path。连接到连接目录和文件名:

for path, subdirs, files in os.walk(root):
    for name in files:
        print os.path.join(path, name)

Note the usage of path and not root in the concatenation, since using root would be incorrect.

注意在连接中使用路径而不是根,因为使用根将是不正确的。


In Python 3.4, the pathlib module was added for easier path manipulations. So the equivalent to os.path.join would be:

在Python 3.4中,为更容易的路径操作添加了pathlib模块。也就是os。path。加入是:

pathlib.PurePath(path, name)

The advantage of pathlib is that you can use a variety of useful methods on paths. If you use the concrete Path variant you can also do actual OS calls through them, like chanding into a directory, deleting the path, opening the file it points to and much more.

pathlib的优点是可以在路径上使用各种有用的方法。如果您使用具体的路径变体,您还可以通过它们进行实际的操作系统调用,比如对目录进行搜索、删除路径、打开它指向的文件等等。

#2


22  

Just in case... Getting all files in the directory and subdirectories matching some pattern (*.py for example):

以防……在目录和子目录中获取与某些模式匹配的所有文件(*。py为例):

import os
from fnmatch import fnmatch

root = '/some/directory'
pattern = "*.py"

for path, subdirs, files in os.walk(root):
    for name in files:
        if fnmatch(name, pattern):
            print os.path.join(path, name)

#3


6  

Here is a one-liner:

import os

[val for sublist in [[os.path.join(i[0], j) for j in i[2]] for i in os.walk('./')] for val in sublist]
# Meta comment to ease selecting text

The outer most val for sublist in ... loop flattens the list to be one dimensional. The j loop collects a list of every file basename and joins it to the current path. Finally, the i loop iterates over all directories and sub directories.

子列表中最外层的val。loop将列表变平为一维。j循环收集每个文件basename的列表并将其连接到当前路径。最后,i循环遍历所有目录和子目录。

This example uses the hard-coded path ./ in the os.walk(...) call, you can supplement any path string you like.

本例使用硬编码路径。/在os.walk(…)调用中,您可以补充任何您喜欢的路径字符串。

Note: os.path.expanduser and/or os.path.expandvars can be used for paths strings like ~/

注意:os.path。expanduser和/或os.path。扩展字符可以用于像~/这样的路径字符串

Extending this example:

Its easy to add in file basename tests and directoryname tests.

它很容易添加到文件basename测试和directoryname测试中。

For Example, testing for *.jpg files:

例如,测试*.jpg文件:

... for j in i[2] if j.endswith('.jpg')] ...

Additionally, excluding the .git directory:

此外,不包括.git目录:

... for i in os.walk('./') if '.git' not in i[0].split('/')]

#4


5  

You should use 'r' in your join instead of 'root'

你应该在连接中使用“r”而不是“root”

#5


1  

You can take a look at this sample I made. It uses the os.path.walk function which is deprecated beware.Uses a list to store all the filepaths

你可以看看我做的这个样品。它使用os.path。不赞成的步行功能请注意。使用列表存储所有文件路径

root = "Your root directory"
ex = ".txt"
where_to = "Wherever you wanna write your file to"
def fileWalker(ext,dirname,names):
    '''
    checks files in names'''
    pat = "*" + ext[0]
    for f in names:
        if fnmatch.fnmatch(f,pat):
            ext[1].append(os.path.join(dirname,f))


def writeTo(fList):

    with open(where_to,"w") as f:
        for di_r in fList:
            f.write(di_r + "\n")






if __name__ == '__main__':
    li = []
    os.path.walk(root,fileWalker,[ex,li])

    writeTo(li)

#6


0  

A bit simpler one-liner:

一点简单的一行程序:

import os
from itertools import product, chain

chain.from_iterable([["\\".join(w) for w in product([i[0]], i[2])] for i in os.walk(dir)])

#1


126  

Use os.path.join to concatenate the directory and file name:

使用os.path。连接到连接目录和文件名:

for path, subdirs, files in os.walk(root):
    for name in files:
        print os.path.join(path, name)

Note the usage of path and not root in the concatenation, since using root would be incorrect.

注意在连接中使用路径而不是根,因为使用根将是不正确的。


In Python 3.4, the pathlib module was added for easier path manipulations. So the equivalent to os.path.join would be:

在Python 3.4中,为更容易的路径操作添加了pathlib模块。也就是os。path。加入是:

pathlib.PurePath(path, name)

The advantage of pathlib is that you can use a variety of useful methods on paths. If you use the concrete Path variant you can also do actual OS calls through them, like chanding into a directory, deleting the path, opening the file it points to and much more.

pathlib的优点是可以在路径上使用各种有用的方法。如果您使用具体的路径变体,您还可以通过它们进行实际的操作系统调用,比如对目录进行搜索、删除路径、打开它指向的文件等等。

#2


22  

Just in case... Getting all files in the directory and subdirectories matching some pattern (*.py for example):

以防……在目录和子目录中获取与某些模式匹配的所有文件(*。py为例):

import os
from fnmatch import fnmatch

root = '/some/directory'
pattern = "*.py"

for path, subdirs, files in os.walk(root):
    for name in files:
        if fnmatch(name, pattern):
            print os.path.join(path, name)

#3


6  

Here is a one-liner:

import os

[val for sublist in [[os.path.join(i[0], j) for j in i[2]] for i in os.walk('./')] for val in sublist]
# Meta comment to ease selecting text

The outer most val for sublist in ... loop flattens the list to be one dimensional. The j loop collects a list of every file basename and joins it to the current path. Finally, the i loop iterates over all directories and sub directories.

子列表中最外层的val。loop将列表变平为一维。j循环收集每个文件basename的列表并将其连接到当前路径。最后,i循环遍历所有目录和子目录。

This example uses the hard-coded path ./ in the os.walk(...) call, you can supplement any path string you like.

本例使用硬编码路径。/在os.walk(…)调用中,您可以补充任何您喜欢的路径字符串。

Note: os.path.expanduser and/or os.path.expandvars can be used for paths strings like ~/

注意:os.path。expanduser和/或os.path。扩展字符可以用于像~/这样的路径字符串

Extending this example:

Its easy to add in file basename tests and directoryname tests.

它很容易添加到文件basename测试和directoryname测试中。

For Example, testing for *.jpg files:

例如,测试*.jpg文件:

... for j in i[2] if j.endswith('.jpg')] ...

Additionally, excluding the .git directory:

此外,不包括.git目录:

... for i in os.walk('./') if '.git' not in i[0].split('/')]

#4


5  

You should use 'r' in your join instead of 'root'

你应该在连接中使用“r”而不是“root”

#5


1  

You can take a look at this sample I made. It uses the os.path.walk function which is deprecated beware.Uses a list to store all the filepaths

你可以看看我做的这个样品。它使用os.path。不赞成的步行功能请注意。使用列表存储所有文件路径

root = "Your root directory"
ex = ".txt"
where_to = "Wherever you wanna write your file to"
def fileWalker(ext,dirname,names):
    '''
    checks files in names'''
    pat = "*" + ext[0]
    for f in names:
        if fnmatch.fnmatch(f,pat):
            ext[1].append(os.path.join(dirname,f))


def writeTo(fList):

    with open(where_to,"w") as f:
        for di_r in fList:
            f.write(di_r + "\n")






if __name__ == '__main__':
    li = []
    os.path.walk(root,fileWalker,[ex,li])

    writeTo(li)

#6


0  

A bit simpler one-liner:

一点简单的一行程序:

import os
from itertools import product, chain

chain.from_iterable([["\\".join(w) for w in product([i[0]], i[2])] for i in os.walk(dir)])