Answers for "get list of file names in folder"

3

get the names of all files in a folder

In MS Windows it works like this:

1. Hold the "Shift" key, right-click the folder containing the files and select "Open Command Window Here."

2. Type "dir /b > filenames.txt" (without quotation marks) in the Command Window. Press "Enter."

3. Inside the folder there should now be a file filenames.txt containing names of all the files etc. inside this folder.

4. Copy and paste this file list into your Word document.
Posted by: Guest on August-01-2020
1

get the list of file names from a folder in python

from os import listdir
from os.path import isfile, join

# Getting file path from user and store it on path variable
path = input("Please enter a directory name:n")

# Try catch for printing error message if the user enter invalid path
try:
    # Getting list of file from the given path and store it in (onlyfiles) variable
  onlyfiles = [f for f in listdir(path) if isfile(join(path, f))]

    # Looping through the file and print each file
  for file in onlyfiles:
    print(file)

except Exception as e:
    # If there is some error, print error message
  print("Incorrect path.")
Posted by: Guest on September-29-2021

Code answers related to "get list of file names in folder"

Browse Popular Code Answers by Language