In Python, I only want to list all the files in the current directory ONLY. I do not want files listed from any subdirectory or parent.
在Python中,我只想列出当前目录中的所有文件。我不希望从任何子目录或父目录中列出文件。
There do seem to be similar solutions out there, but they don't seem to work for me. Here's my code snippet:
似乎确实有类似的解决方案,但它们似乎对我不起作用。这是我的代码片段:
import os
for subdir, dirs, files in os.walk('./'):
for file in files:
do some stuff
print file
Let's suppose I have 2 files, holygrail.py and Tim inside my current directory. I have a folder as well and it contains two files - let's call them Arthur and Lancelot - inside it. When I run the script, this is what I get:
假设我有两个文件,holygrail。py和Tim在当前目录下。我也有一个文件夹,里面有两个文件,叫亚瑟和兰斯洛特。当我运行脚本时,我得到的是:
holygrail.py
Tim
Arthur
Lancelot
I am happy with holygrail.py and Tim. But the two files, Arthur and Lancelot, I do not want listed.
我对圣杯很满意。py和蒂姆。但是亚瑟和兰斯洛特这两个文件,我不想列出来。
8 个解决方案
#1
259
Just use os.listdir
and os.path.isfile
instead of os.walk
.
用操作系统。listdir os.path。isfile代替os.walk。
Example:
例子:
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
# do something
But be careful while applying this to other directory, like
但是在将它应用到其他目录时要小心,比如
files = [f for f in os.listdir(somedir) if os.path.isfile(f)].
which would not work because f
is not a full path but relative to the current dir.
这将不起作用,因为f不是完整的路径,而是相对于当前的dir。
Therefore, for filtering on another directory, do os.path.isfile(os.path.join(somedir, f))
因此,要过滤另一个目录,请执行os.path.isfile(os.path)。加入(somedir f))
(Thanks Causality for the hint)
(感谢因果关系的暗示)
#2
46
You can use os.listdir
for this purpose. If you only want files and not directories, you can filter the results using os.path.isfile
.
您可以使用操作系统。为此listdir。如果只需要文件而不是目录,则可以使用os.path.isfile筛选结果。
example:
例子:
files = os.listdir(os.curdir) #files and directories
or
或
files = filter(os.path.isfile, os.listdir( os.curdir ) ) # files only
files = [ f for f in os.listdir( os.curdir ) if os.path.isfile(f) ] #list comprehension version.
#3
9
import os
destdir = '/var/tmp/testdir'
files = [ f for f in os.listdir(destdir) if os.path.isfile(os.path.join(destdir,f)) ]
#4
3
You can use os.scandir()
. New function in stdlib starts from Python 3.5.
您可以使用os.scandir()。stdlib中的新函数从Python 3.5开始。
import os
for entry in os.scandir('.'):
if entry.is_file():
print(entry.name)
Faster than os.listdir()
. os.walk()
implements os.scandir()
.
速度比os.listdir()。实现os.scandir os.walk()()。
#5
3
this can be done with os.walk()
这可以用os.walk()来完成
python 3.5.2 tested;
python 3.5.2测试;
import os
for root, dirs, files in os.walk('.', topdown=True):
dirs.clear() #with topdown true, this will prevent walk from going into subs
for file in files:
#do some stuff
print(file)
remove the dirs.clear() line and the files in sub folders are included again.
删除dirs.clear()行并再次包含子文件夹中的文件。
update with references;
与引用更新;
os.walk documented here and talks about the triple list being created and topdown effects.
操作系统。walk在这里记录并讨论正在创建的三重列表和自上而下的效果。
.clear() documented here for emptying a list
.clear()记录在这里,用于清空列表。
so by clearing the relevant list from os.walk you can effect its result to your needs.
通过从操作系统中清除相关列表。你可以根据自己的需要来决定结果。
#6
2
instead of os.walk
, just use os.listdir
而不是操作系统。走,只使用os.listdir
#7
2
import os
for subdir, dirs, files in os.walk('./'):
for file in files:
do some stuff
print file
You can improve this code with del dirs[:]
which will be like following .
您可以使用del dirs[:]来改进这段代码,如下所示。
import os
for subdir, dirs, files in os.walk('./'):
del dirs[:]
for file in files:
do some stuff
print file
Or even better if you could point os.walk with current working directory .
或者如果你能点os。执行当前工作目录。
import os
cwd = os.getcwd()
for subdir, dirs, files in os.walk(cwd, topdown=True):
del dirs[:] # remove the sub directories.
for file in files:
do some stuff
print file
#8
-3
My solution is:
我的解决方案是:
import re
p = re.compile('[a-z]+', re.IGNORECASE)
words = p.findall("Hello, world! I'm a coder")
I think this solution is better
我认为这个解决方案更好
#1
259
Just use os.listdir
and os.path.isfile
instead of os.walk
.
用操作系统。listdir os.path。isfile代替os.walk。
Example:
例子:
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
# do something
But be careful while applying this to other directory, like
但是在将它应用到其他目录时要小心,比如
files = [f for f in os.listdir(somedir) if os.path.isfile(f)].
which would not work because f
is not a full path but relative to the current dir.
这将不起作用,因为f不是完整的路径,而是相对于当前的dir。
Therefore, for filtering on another directory, do os.path.isfile(os.path.join(somedir, f))
因此,要过滤另一个目录,请执行os.path.isfile(os.path)。加入(somedir f))
(Thanks Causality for the hint)
(感谢因果关系的暗示)
#2
46
You can use os.listdir
for this purpose. If you only want files and not directories, you can filter the results using os.path.isfile
.
您可以使用操作系统。为此listdir。如果只需要文件而不是目录,则可以使用os.path.isfile筛选结果。
example:
例子:
files = os.listdir(os.curdir) #files and directories
or
或
files = filter(os.path.isfile, os.listdir( os.curdir ) ) # files only
files = [ f for f in os.listdir( os.curdir ) if os.path.isfile(f) ] #list comprehension version.
#3
9
import os
destdir = '/var/tmp/testdir'
files = [ f for f in os.listdir(destdir) if os.path.isfile(os.path.join(destdir,f)) ]
#4
3
You can use os.scandir()
. New function in stdlib starts from Python 3.5.
您可以使用os.scandir()。stdlib中的新函数从Python 3.5开始。
import os
for entry in os.scandir('.'):
if entry.is_file():
print(entry.name)
Faster than os.listdir()
. os.walk()
implements os.scandir()
.
速度比os.listdir()。实现os.scandir os.walk()()。
#5
3
this can be done with os.walk()
这可以用os.walk()来完成
python 3.5.2 tested;
python 3.5.2测试;
import os
for root, dirs, files in os.walk('.', topdown=True):
dirs.clear() #with topdown true, this will prevent walk from going into subs
for file in files:
#do some stuff
print(file)
remove the dirs.clear() line and the files in sub folders are included again.
删除dirs.clear()行并再次包含子文件夹中的文件。
update with references;
与引用更新;
os.walk documented here and talks about the triple list being created and topdown effects.
操作系统。walk在这里记录并讨论正在创建的三重列表和自上而下的效果。
.clear() documented here for emptying a list
.clear()记录在这里,用于清空列表。
so by clearing the relevant list from os.walk you can effect its result to your needs.
通过从操作系统中清除相关列表。你可以根据自己的需要来决定结果。
#6
2
instead of os.walk
, just use os.listdir
而不是操作系统。走,只使用os.listdir
#7
2
import os
for subdir, dirs, files in os.walk('./'):
for file in files:
do some stuff
print file
You can improve this code with del dirs[:]
which will be like following .
您可以使用del dirs[:]来改进这段代码,如下所示。
import os
for subdir, dirs, files in os.walk('./'):
del dirs[:]
for file in files:
do some stuff
print file
Or even better if you could point os.walk with current working directory .
或者如果你能点os。执行当前工作目录。
import os
cwd = os.getcwd()
for subdir, dirs, files in os.walk(cwd, topdown=True):
del dirs[:] # remove the sub directories.
for file in files:
do some stuff
print file
#8
-3
My solution is:
我的解决方案是:
import re
p = re.compile('[a-z]+', re.IGNORECASE)
words = p.findall("Hello, world! I'm a coder")
I think this solution is better
我认为这个解决方案更好