Quantcast
Channel: Python list directory, subdirectory, and files - Stack Overflow
Browsing all 12 articles
Browse latest View live

Answer by Martin Thoma for Python list directory, subdirectory, and files

Using any supported Python version (3.4+), you should use pathlib.rglob to recursively list the contents of the current directory and all subdirectories:from pathlib import Pathdef...

View Article



Answer by Rotareti for Python list directory, subdirectory, and files

Another option would be using the glob module from the standard library:import globpath = "/home/patate/directory/targetdirectory/**"for path in glob.glob(path, recursive=True): print(path)If you need...

View Article

Answer by Chadee Fouad for Python list directory, subdirectory, and files

And this is how you list it in case you want to list the files on SharePoint. Your path will probably start after the "\teams\" part.import osroot =...

View Article

Answer by CMSG for Python list directory, subdirectory, and files

A pretty simple solution would be to run a couple of sub process calls to export the files into CSV format:import subprocess# Global variables for directory being mappedlocation = '.' # Enter the path...

View Article

Answer by kiran beethoju for Python list directory, subdirectory, and files

It's just an addition. With this, you can get the data into CSV format:import sys, ostry: import pandas as pdexcept: os.system("pip3 install pandas")root = "/home/kiran/Downloads/MainFolder" # It may...

View Article


Answer by Puddle for Python list directory, subdirectory, and files

Since every example here is just using walk (with join), I'd like to show a nice example and comparison with listdir:import os, timedef listFiles1(root): # listdir allFiles = []; walk = [root] while...

View Article

Answer by Daniel for Python list directory, subdirectory, and files

A bit simpler one-liner:import osfrom itertools import product, chainchain.from_iterable([[os.sep.join(w) for w in product([i[0]], i[2])] for i in os.walk(dir)])

View Article

Answer by ThorSummoner for Python list directory, subdirectory, and files

Here is a one-liner:import os[val for sublist in [[os.path.join(i[0], j) for j in i[2]] for i in os.walk('./')] for val in sublist]# Meta comment to ease selecting textThe outer most val for sublist in...

View Article


Answer by devsaw for Python list directory, subdirectory, and files

You can take a look at this sample I made. It uses the os.path.walk function which is deprecated beware. It uses a list to store all the filepaths.root = "Your root directory"ex = ".txt"where_to =...

View Article


Answer by Ivan Pirog for Python list directory, subdirectory, and files

Just in case... Getting all files in the directory and subdirectories matching some pattern (*.py for example):import osfrom fnmatch import fnmatchroot = '/some/directory'pattern = "*.py"for path,...

View Article

Answer by Eli Bendersky for Python list directory, subdirectory, and files

Use os.path.join to concatenate the directory and file name:import osfor path, subdirs, files in os.walk(root): for name in files: print(os.path.join(path, name))Note the usage of path and not root in...

View Article

Python list directory, subdirectory, and files

I'm trying to make a script to list all directories, subdirectories, and files in a given directory.I tried this:import sys, osroot = "/home/patate/directory/"path = os.path.join(root,...

View Article
Browsing all 12 articles
Browse latest View live


Latest Images