Home > database >  Get all files on disk in python
Get all files on disk in python

Time:09-22

im creating script in python that get all files on disk, but no folders only files. Its my code.

import hashlib
import os

if os.name != "nt":
    print("Sorry this script works only on Windows!")

path = "C://"
dir_list = os.listdir(path)
print(dir_list)

CodePudding user response:

You can use for example the pathlib library and build something like that:

import pathlib

path = "" # add your path here don't forget a \ at the end on windows
for i in os.listdir(path):
    if pathlib.Path(path   i).is_dir() is not True:
         print(i)

It iterates through the current directory and checks if its a directory, by creating a Path object from the list entry and then checks on that object if it is a directory.

  • Related