This question already has answers here:
Python recursive folder read
(16 answers)
Closed 9 years ago.
I'd like to get a list of all files in a directory recursively, with no directories.
Say there's a directory ~/files with "a.txt", "b.txt", and a directory "c" with "d.txt" and "e" inside it, and "f.txt" inside e. How would I go about getting a list that looks like ['/home/user/files/a.txt', '/home/user/files/b.txt', '/home/user/files/c/d.txt', '/home/user/files/c/e/f.txt']?
import os
[os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser("~/files")) for f in fn]
Related
This question already has answers here:
Relative paths in Python [duplicate]
(21 answers)
what does the __file__ variable mean/do?
(6 answers)
Closed 1 year ago.
For example:
in the file main/main.py
import core.fun as f
f.script()
and in the file main/core/fun.py:
def script():
f = open('test.txt','r')
where a file test.txt is present in the folder main/core/.
If I run main/main.py it will not work because python will search for the file 'test.txt' in the main folder and not in the main/core folder.
And if I call within the function script()
import os
os.getcwd()
It will again return the directory main
Therefore I would like to know how can I find the directory of the function within the function.
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.
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.
This question already has answers here:
Directory-tree listing in Python
(21 answers)
Closed 9 years ago.
I want to retrieve the filenames of all the files with .xml extension present in various subfolders in a single directory,
code:
import os
xmlFiles = []
for directoryPath in os.walk(filePath):
fileName = directoryPath[2]
if fileName[:3] = 'xml':# or fileName.endswith('xml'):
xmlFiles.append(fileName)
Use os.walk, and str.endswith.
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.