아래 디렉토리를 파지 않고 os.walk
os.walk
내가 제공 한 디렉토리의 파일 만 반환하도록 제한 하려면 어떻게합니까 ?
def _dir_list(self, dir_name, whitelist):
outputList = []
for root, dirs, files in os.walk(dir_name):
for f in files:
if os.path.splitext(f)[1] in whitelist:
outputList.append(os.path.join(root, f))
else:
self._email_to_("ignore")
return outputList
walklevel
기능을 사용하십시오 .
import os
def walklevel(some_dir, level=1):
some_dir = some_dir.rstrip(os.path.sep)
assert os.path.isdir(some_dir)
num_sep = some_dir.count(os.path.sep)
for root, dirs, files in os.walk(some_dir):
yield root, dirs, files
num_sep_this = root.count(os.path.sep)
if num_sep + level <= num_sep_this:
del dirs[:]
처럼 작동 os.walk
하지만 level
재귀의 깊이를 나타내는 매개 변수를 전달할 수 있습니다 .
os.walk를 사용하지 마십시오.
예:
import os
root = "C:\\"
for item in os.listdir(root):
if os.path.isfile(os.path.join(root, item)):
print item
해결책은 실제로 매우 간단하다고 생각합니다.
사용하다
break
for 루프의 첫 번째 반복 만 수행하려면 더 우아한 방법이 있어야합니다.
for root, dirs, files in os.walk(dir_name):
for f in files:
...
...
break
...
os.walk를 처음 호출하면 현재 디렉토리에 대한 튤립을 반환 한 다음 다음 디렉토리의 내용을 루프합니다.
원본 스크립트를 가져 와서 휴식을 추가하십시오 .
def _dir_list(self, dir_name, whitelist):
outputList = []
for root, dirs, files in os.walk(dir_name):
for f in files:
if os.path.splitext(f)[1] in whitelist:
outputList.append(os.path.join(root, f))
else:
self._email_to_("ignore")
break
return outputList
사용 제안 listdir
은 좋은 것입니다. Python 2에서 귀하의 질문에 대한 직접적인 대답은 root, dirs, files = os.walk(dir_name).next()
.
동등한 Python 3 구문은 다음과 같습니다. root, dirs, files = next(os.walk(dir_name))
You could use os.listdir()
which returns a list of names (for both files and directories) in a given directory. If you need to distinguish between files and directories, call os.stat()
on each name.
If you have more complex requirements than just the top directory (eg ignore VCS dirs etc), you can also modify the list of directories to prevent os.walk recursing through them.
ie:
def _dir_list(self, dir_name, whitelist):
outputList = []
for root, dirs, files in os.walk(dir_name):
dirs[:] = [d for d in dirs if is_good(d)]
for f in files:
do_stuff()
Note - be careful to mutate the list, rather than just rebind it. Obviously os.walk doesn't know about the external rebinding.
The same idea with listdir
, but shorter:
[f for f in os.listdir(root_dir) if os.path.isfile(os.path.join(root_dir, f))]
for path, dirs, files in os.walk('.'):
print path, dirs, files
del dirs[:] # go only one level deep
Felt like throwing my 2 pence in.
baselevel = len(rootdir.split("\\"))
for subdirs, dirs, files in os.walk(rootdir):
curlevel = len(subdirs.split("\\"))
if curlevel <= baselevel + 1:
[do stuff]
In Python 3, I was able to do this:
import os
dir = "/path/to/files/"
#List all files immediately under this folder:
print ( next( os.walk(dir) )[2] )
#List all folders immediately under this folder:
print ( next( os.walk(dir) )[1] )
You could also do the following:
for path, subdirs, files in os.walk(dir_name):
for name in files:
if path == ".": #this will filter the files in the current directory
#code here
Since Python 3.5 you can use os.scandir
instead of os.listdir
. Instead of strings you get an iterator of DirEntry
objects in return. From the docs:
Using
scandir()
instead oflistdir()
can significantly increase the performance of code that also needs file type or file attribute information, becauseDirEntry
objects expose this information if the operating system provides it when scanning a directory. AllDirEntry
methods may perform a system call, butis_dir()
andis_file()
usually only require a system call for symbolic links;DirEntry.stat()
always requires a system call on Unix but only requires one for symbolic links on Windows.
You can access the name of the object via DirEntry.name
which is then equivalent to the output of os.listdir
This is how I solved it
if recursive:
items = os.walk(target_directory)
else:
items = [next(os.walk(target_directory))]
...
There is a catch when using listdir. The os.path.isdir(identifier) must be an absolute path. To pick subdirectories you do:
for dirname in os.listdir(rootdir):
if os.path.isdir(os.path.join(rootdir, dirname)):
print("I got a subdirectory: %s" % dirname)
The alternative is to change to the directory to do the testing without the os.path.join().
You can use this snippet
for root, dirs, files in os.walk(directory):
if level > 0:
# do some stuff
else:
break
level-=1
create a list of excludes, use fnmatch to skip the directory structure and do the process
excludes= ['a\*\b', 'c\d\e']
for root, directories, files in os.walk('Start_Folder'):
if not any(fnmatch.fnmatch(nf_root, pattern) for pattern in excludes):
for root, directories, files in os.walk(nf_root):
....
do the process
....
same as for 'includes':
if **any**(fnmatch.fnmatch(nf_root, pattern) for pattern in **includes**):
Why not simply use a range
and os.walk
combined with the zip
? Is not the best solution, but would work too.
For example like this:
# your part before
for count, (root, dirs, files) in zip(range(0, 1), os.walk(dir_name)):
# logic stuff
# your later part
Works for me on python 3.
Also: A break
is simpler too btw. (Look at the answer from @Pieter)
A slight change to Alex's answer, but using __next__()
:
print(next(os.walk('d:/'))[2])
or print(os.walk('d:/').__next__()[2])
with the [2]
being the file
in root, dirs, file
mentioned in other answers
root folder changes for every directory os.walk finds. I solver that checking if root == directory
def _dir_list(self, dir_name, whitelist):
outputList = []
for root, dirs, files in os.walk(dir_name):
if root == dir_name: #This only meet parent folder
for f in files:
if os.path.splitext(f)[1] in whitelist:
outputList.append(os.path.join(root, f))
else:
self._email_to_("ignore")
return outputList
import os
def listFiles(self, dir_name):
names = []
for root, directory, files in os.walk(dir_name):
if root == dir_name:
for name in files:
names.append(name)
return names
참고URL : https://stackoverflow.com/questions/229186/os-walk-without-digging-into-directories-below
'Programing' 카테고리의 다른 글
gcc가 C에서 일부 명령문을 최적화하는 것을 방지하는 방법은 무엇입니까? (0) | 2020.08.27 |
---|---|
GSON을 사용하여 JSON 스타일 속성 이름을 Java CamelCase 이름으로 변환 (0) | 2020.08.27 |
NSString에서 문자를 제거 하시겠습니까? (0) | 2020.08.27 |
sqlite 쿼리에서 dict를 어떻게 얻을 수 있습니까? (0) | 2020.08.26 |
Android에서 GSON 또는 다른 라이브러리를 사용하지 않고 개조를 사용하여 문자열로 응답을 얻는 방법 (0) | 2020.08.26 |