python - Comment puis-je lister tous les fichiers d'un répertoire

Mots clés : pythondirectorypython

meilleur 5 Réponses python - Comment puis-je lister tous les fichiers d'un répertoire

vote vote

100

from os import listdir from os.path import isfile, join onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))] 
from os import walk  f = [] for (dirpath, dirnames, filenames) in walk(mypath):     f.extend(filenames)     break 
from os import walk  filenames = next(walk(mypath), (None, None, []))[2]  # [] if no file 
vote vote

86

import glob print(glob.glob("/home/adam/*")) 
import glob # All files ending with .txt print(glob.glob("/home/adam/*.txt"))  # All files ending with .txt with depth of 2 folder print(glob.glob("/home/adam/*/*.txt"))  
['/home/adam/file1.txt', '/home/adam/file2.txt', .... ] 
vote vote

75

 import os   arr = os.listdir() 
arr = os.listdir('c:\\files') 
import glob  txtfiles = [] for file in glob.glob("*.txt"):     txtfiles.append(file) 
mylist = [f for f in glob.glob("*.txt")] 
import os from os import listdir from os.path import isfile, join  cwd = os.getcwd() onlyfiles = [os.path.join(cwd, f) for f in os.listdir(cwd) if  os.path.isfile(os.path.join(cwd, f))] print(onlyfiles)   ['G:\\getfilesname\\getfilesname.py', 'G:\\getfilesname\\example.txt'] 
 import os  files_path = [os.path.abspath(x) for x in os.listdir()]  print(files_path)    ['F:\\documenti\applications.txt', 'F:\\documenti\collections.txt'] 
import os  # Getting the current work directory (cwd) thisdir = os.getcwd()  # r=root, d=directories, f = files for r, d, f in os.walk(thisdir):     for file in f:         if file.endswith(".docx"):             print(os.path.join(r, file)) 
# Method 1 x = os.listdir('..')  # Method 2 x= os.listdir('/') 
import os  x = os.listdir("./content") 
 import os  arr = next(os.walk('.'))[2]  print(arr)    >>> ['5bs_Turismo1.pdf', '5bs_Turismo1.pptx', 'esperienza.txt'] 
 import os  arr = []  for d,r,f in next(os.walk("F:\\_python")):      for file in f:          arr.append(os.path.join(r,file))   for f in arr:      print(files)  >>> F:\\_python\\dict_class.py >>> F:\\_python\\programmi.txt 
 [os.path.join(r,file) for r,d,f in next(os.walk("F:\\_python")) for file in f]    >>> ['F:\\_python\\dict_class.py', 'F:\\_python\\programmi.txt'] 
x = [os.path.join(r,file) for r,d,f in os.walk("F:\\_python") for file in f] print(x)  >>> ['F:\\_python\\dict.py', 'F:\\_python\\progr.txt', 'F:\\_python\\readl.py'] 
 arr_txt = [x for x in os.listdir() if x.endswith(".txt")]   
from path import path from glob import glob  x = [path(f).abspath() for f in glob("F:\\*.txt")] 
import os.path listOfFiles = [f for f in os.listdir() if os.path.isfile(f)] 
import pathlib  flist = [] for p in pathlib.Path('.').iterdir():     if p.is_file():         print(p)         flist.append(p) 
flist = [p for p in pathlib.Path('.').iterdir() if p.is_file()] 
import pathlib  py = pathlib.Path().glob("*.py") 
import os x = [i[2] for i in os.walk('.')] y=[] for t in x:     for f in t:         y.append(f) 
 import os  x = next(os.walk('F://python'))[2] 
 import os  next(os.walk('F://python'))[1] # for the current dir use ('.')    >>> ['python3','others'] 
for r,d,f in os.walk("F:\\_python"):     for dirs in d:         print(dirs) 
import os x = [f.name for f in os.scandir() if f.is_file()]  # Another example with scandir (a little variation from docs.python.org) # This one is more efficient than os.listdir. # In this case, it shows the files only in the current directory # where the script is executed.  import os with os.scandir() as i:     for entry in i:         if entry.is_file():             print(entry.name) 
vote vote

61

import os os.listdir("somedirectory") 
vote vote

57

filenames = next(os.walk(path))[2] 
paths = [os.path.join(path, fn) for fn in next(os.walk(path))[2]] 

Questions similaires