Why glob is not getting files in subfolders [duplicate] - python

This question already has answers here:
How to use glob() to find files recursively?
(28 answers)
Closed 17 days ago.
I have following directory structure
$ find
.
./file1.html
./soquest_glob.py
./dir1
./dir1/file2.html
./dir2
./dir2/file3.html
(I have added blank lines above to clarify files in different folders).
I am trying to find all html files (including those in subfolders) with following Python code using glob package:
$ cat soquest_glob.py
import glob
flist = glob.glob("*.html", recursive=True)
print(flist)
However, when I run this code, it finds only file in current folder, not in subfolders:
$ python3 soquest_glob.py
['file1.html']
Where is the problem and how can it be solved?

The recursive argument to glob.glob effects the behavior of **. You need to use a pattern like: glob.glob("**/*.html", recursive=True).

Related

How to get all files in a directory? [duplicate]

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.

Scanning a directory to list the name of all files [duplicate]

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']

Python check if a directory exists, then create it if necessary and save graph to new directory? [duplicate]

This question already has answers here:
How can I safely create a directory (possibly including intermediate directories)?
(28 answers)
Closed 5 years ago.
so I want this to be independent of the computer the code is used on, so I want to be able to create a directory in the current directory and save my plots to that new file. I looked at some other questions and tried this (I have two attempts, one commented out):
import os
from os import path
#trying to make shift_graphs directory if it does not already exist:
if not os.path.exists('shift_graphs'):
os.mkdirs('shift_graphs')
plt.title('Shift by position on '+str(detector_num)+'-Detector')
#saving figure to shift_graphs directory
plt.savefig(os.path.join('shift_graphs','shift by position on '+str(detector_num)+'-detector'))
print "plot 5 done"
plt.clf
I get the error :
AttributeError: 'module' object has no attribute 'mkdirs'
I also want to know if my idea of saving it in the directory will work, which I haven't been able to test because of the errors I've been getting in the above portion.
os.mkdirs() is not a method in os module.
if you are making only one direcory then use os.mkdir() and if there are multiple directories try using os.makedirs()
Check Documentation
You are looking for either:
os.mkdir
Or os.makedirs
https://docs.python.org/2/library/os.html
os.makedirs makes all the directories, so if I type in shell (and get nothing):
$ ls
$ python
>>> import os
>>> os.listdir(os.getcwd())
[]
>>> os.makedirs('alex/is/making/a/path')
>>> os.listdir(os.getcwd())
['alex']
It has made all the directories and subdirectories. os.mkdir would throw me an error, because there is no "alex/is/making/a" directory.

Using Python, how do you remove files given a filespec such as "*.obj" on Windows? [duplicate]

This question already has answers here:
Get a filtered list of files in a directory
(14 answers)
Closed 8 years ago.
How do you remove files given a filespec such as "*.obj" on Windows? I'm using Windows 7 and 8.1 at the moment.
Evidently os.remove does not take filespecs ("filespec" being a crude regular-expression for including wildcards such as *.txt to mean all files that end with ".txt").
The python glob module provides wildcard file matching. So
import glob
import os
for f in glob.glob("*.obj"):
os.remove(f)

How to find what files are in a folder [duplicate]

This question already has answers here:
Directory-tree listing in Python
(21 answers)
Closed 9 years ago.
I am writing a function that is recieving a folder path as an arguemnt. I want her to add into a dictionary what's inside the folder (like dir in CMD)
How can I do this ?
Thank you in advance,
Iliya
import os
print os.listdir('/tmp')
Similar Topics:
Directory listing in Python
Also, I use os.path and glob a lot while manipulating file system path.
You might want to check it out.

Categories