Read structure in files python - python

I need to populate a txt with the structure in certain directory, i read the files in the directory:
os.listdir(r)
But if one item is a directory i need to search into it, any idea?

Use os.walk. It will recursively traverse paths. ex:
import os
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
print(os.path.join(root, name))

You can use os.walk.
for root,dirs,files in os.walk(directory):
...

Related

Ignore files from specific directory using glob or by os.walk()

I want to exclude the directory 'dir3_txt' so that I can only capture the files('.txt') from other directory . I tried to exclude directory like below but not able to figure it out how to get all the files having .txt as ext other that having it in dir3_txt using below:
for root, dirs, files in os.walk('.'):
print (root)
dirs[:] = [d for d in dirs if not d.startswith('dir3')]
for file in files:
print (os.path.join(root, file))
I am thinking of glob (got below from stack itself) but not sure how to tweak glob to use it.
for file in os.walk('.'):
for txt in glob(os.path.join(files[0], '*.txt')):
print(txt)
I went through Excluding directories in os.walk but the solution provided is not helping me, also it only tells about skipping directory that also is not helpful as I need to get files from other directories , better if we can do it with glob only?
A simple solution would just to do a string comparison against the directory-paths and files returned by os.walk:
for root, dirs, files in os.walk('.'):
if "/dir3_txt/" not in root:
for file in files:
if file.endswith(".txt"):
print (os.path.join(root, file))
for root, dirs, files in os.walk('.'):
print (root)
dirs[:]= [d for d in dirs if d[:4]!='dir3']
for file in files:
if file[-4:]=='.txt':
print (os.path.join(root, file))
I dont have any system with me now to test this , so if any problems please comment.
Edit:
Now it only detects '.txt' files.

iterate a directory and find only file whose names start with the certain string

I have a directory path and in this path there are several folders. So i am am trying to build a script which would find all the xml files and the file name must start with report. I have been so far able to iterate over all the directories but further i do not know how to proceed. Here is my code:
def search_xml_report(rootdir):
for subdir, dirs, files in os.walk(rootdir):
for file in files:
print os.path.join(subdir,file) # print statement just for testing
You can use str.startswith:
def search_xml_report(rootdir):
for subdir, dirs, files in os.walk(rootdir):
for file in files:
if file.startswith('report'):
yield subdir, file
use str.startswith with os.path.splitext
os.path.splitext: Split the extension from a pathname. Extension is everything from the last dot to the end, ignoring leading dots. Returns "(root, ext)"; ext may be empty.
if file.startswith('report') and os.path.splitext(filepath+filename)[-1] == '.xml':
return file

How to list all Block files, Character device files,pipe files,Socket files recursively with pure python codes instead of bash?

There are seven file types in linux: Regular files,Directory files,Block file, Character device file,pipe file,Symbolic link file,Socket file.
All the files and direcotries can be listed recursively.
import os
for root, dirs, files in os.walk("/home", topdown=False):
for name in files:
if os.path.isfile(os.path.join(root,name)):print(os.path.join(root,name))
for root, dirs, files in os.walk("/home/", topdown=False):
for name in dirs:
if os.path.isdir(os.path.join(root,name)):print(os.path.join(root,name))
All links can be listed recursively with python.
import os
for root, dirs, files in os.walk("/home", topdown=False):
for name in files:
if os.path.islink(os.path.join(root,name)):print(os.path.join(root,name))
How to list all Block files, Character device files,pipe files,Socket files recursively with pure python codes instead of bash?
You have os.walk() already working there, so the missing piece is using the os.stat function and the stat module to figure out what type each file is.

Subfolder traversing us os.walk in python

I am trying to make a python code that traverse all subfolders in a parent folder and moves the subfolder contents to the parent folder.I used os.walk function in python but it keeps selecting the files in the parent folder as well.Is there a way to solve this..
for current_folder,subfolders_in_cwd,files_in_cwd in os.walk(some_dir_path):
print "Folders in %s = %s"%(current_folder,subfolders_in_cwd)
import os
PATH = '/home/pi/Public/'
for root, folder, files in os.walk(PATH):
files.sort(key=str.lower) #Sorts files alphabetically
subFolder.sort()
for item in files:
fileNamePath = os.path.join(root, item)
print(fileNamePath)

Is there existing any utilities to get all absolute path of leaf file under a given directory?

os.listdir(path) can return all the files under the directory of the given path. I an wonder if there exists any utility function that can conduct deep traverse and return all the paths of leaf files under a given directory.
Sure! You are looking for os.walk:
import os
for root, dirs, files in os.walk(path):
for filename in files:
filename = os.path.join(root, filename)
print(filename)

Categories

Resources