This question already has answers here:
What is the purpose of the single underscore "_" variable in Python?
(5 answers)
Closed 4 years ago.
Can someone explain the usage of the _ in this for loop?
for dirs,_,files in os.walk(directory):
for f in files:
yield os.path.abspath(os.path.join(dirs, f))
My goal is to get the filenames with full path recursively.
I got this from another question and it does exactly what I want. But I don't understand it.
os.walk returns the tuple (root, dirs, files) where
root: the current directory
dirs: the files in the current dir
files: the files in the current dir
if you do not use one of these variables in your subsequent loop, it is customary to call it _ (or even append a name such as _dirs). that way most IDEs will not complain that you have assigned a variable but you are not using it.
in your example you could do:
for root, _dirs, files in os.walk(directory):
pass
and the IDE should not complain that you are not using the variable _dirs.
Related
This question already has answers here:
List only files in a directory?
(8 answers)
how to check if a file is a directory or regular file in python? [duplicate]
(4 answers)
Closed 2 months ago.
I have a directory and need to get all files in it, but not subdirectories.
I have found os.listdir(path) but that gets subdirectories as well.
My current temporary solution is to then filter the list to include only the things with '.' in the title (since files are expected to have extensions, .txt and such) but that is obviously not optimal.
We can create an empty list called my_files and iterate through the files in the directory. The for loop checks to see if the current iterated file is not a directory. If it is not a directory, it must be a file.
my_files = []
for i in os.listdir(path):
if not os.path.isdir(i):
my_files.append(i)
That being said, you can also check if it is a file instead of checking if it is not a directory, by using if os.path.isfile(i).
I find this approach is simpler than glob because you do not have to deal with any path joining.
This question already has answers here:
How to do a recursive sub-folder search and return files in a list?
(13 answers)
Closed 2 years ago.
I have a big folder in which there are many subfolders. Each of those subfolders can also have some subfolders ( number can be different ) in it. This goes on for some levels. And in the end, there is a text file. How can I make a python program that traverses the entire directory as deep as it goes and prints the name of the text file? In simpler terms, I want to navigate through the directory as long as there are no more sub-directories?
Use os.walk.
For instance, creating a deep hierarchy like
$ mkdir -p a/b/c/d/e/f/g
$ touch a/b/c/d/e/f/g/h.txt
and running
import os
for dirname, dirnames, filenames in os.walk('.'):
for filename in filenames:
filepath = os.path.join(dirname, filename)
print(filepath)
yields
./a/b/c/d/e/f/g/h.txt
– do what you will with filepath.
Another option, if you're using Python 3.5 or newer (and you probably should be) is glob.glob() with a recursive pattern:
>>> print(glob.glob("./**/*.txt", recursive=True))
['./a/b/c/d/e/f/g/h.txt']
This question already has answers here:
How to list only top level directories in Python?
(21 answers)
Closed 2 years ago.
How can I bring python to only output directories via os.listdir, while specifying which directory to list via raw_input?
What I have:
file_to_search = raw_input("which file to search?\n>")
dirlist=[]
for filename in os.listdir(file_to_search):
if os.path.isdir(filename) == True:
dirlist.append(filename)
print dirlist
Now this actually works if I input (via raw_input) the current working directory. However, if I put in anything else, the list returns empty. I tried to divide and conquer this problem but individually every code piece works as intended.
that's expected, since os.listdir only returns the names of the files/dirs, so objects are not found, unless you're running it in the current directory.
You have to join to scanned directory to compute the full path for it to work:
for filename in os.listdir(file_to_search):
if os.path.isdir(os.path.join(file_to_search,filename)):
dirlist.append(filename)
note the list comprehension version:
dirlist = [filename for filename in os.listdir(file_to_search) if os.path.isdir(os.path.join(file_to_search,filename))]
This question already has answers here:
Monitoring contents of files/directories? [duplicate]
(5 answers)
Closed 8 years ago.
How can I iterate all files and subdirs in a Dir,
and can detect new file when put there?
Thank you for your help!
Try os.walk. More specifically, try:
top="."
import os
for root, dirs, files in os.walk(top):
for name in files:
# do something with each file as 'name' (a)
pass
for name in dirs:
# do something with each subdir as 'name' (b)
pass
# do something with root (dir path so far)
# break at any point if necessary
To answer the question in your comment, at point (b) in the code, you can handle any subdirectory logic (also you can check to test that you have the right subdirectory to do certain custom logic on), via another function or directly/inline.
This question already has answers here:
os.walk() ValueError: need more than 1 value to unpack
(4 answers)
Closed 9 years ago.
Can I somehow save the output of os.walk() in variables ? I tried
basepath, directories, files = os.walk(path)
but it didn't work. I want to proceed the files of the directory and one specific subdirectory. is this somehow possible ? Thanks
os.walk() returns a generator that will successively return all the tree of files/directories from the initial path it started on. If you only want to process the files in a directory and one specific subdirectory you should use a mix of os.listdir() and a mixture of os.path.isfile() and os.path.isdir() to get what you want.
Something like this:
def files_and_subdirectories(root_path):
files = []
directories = []
for f in os.listdir(root_path):
if os.path.isfile(f):
files.append(f)
elif os.path.isdir(f):
directories.append(f)
return directories, files
And use it like so:
directories,files = files_and_subdirectories(path)
I want to proceed the files of the directory and one specific
subdirectory. is this somehow possible ?
If that's all you want then simply try
[e for e in os.listdir('.') if os.path.isfile(e)] + os.listdir(special_dir)