-1

I am new to python. I want to create a for loop that prints all the file names in my folder.

This is what I have :

import cf 
f = cf.read('/home/cd_files')
for i in f: 
  print f 

The file names are string values. I am guessing what I failed to include in my code.

Said
  • 689
  • 6
  • 20
  • 6
    Possible duplicate of [How do I list all files of a directory?](https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory) – shaik moeed May 29 '19 at 06:24
  • how deep do you want to print files. Like a folder can contain many other folders and then there are files. Do you want to print files recursively or just from inside one folder? – xxbinxx May 29 '19 at 06:36

3 Answers3

2
import os
for file in os.listdir('your/directory'):
    print(file)
Heike
  • 24,102
  • 2
  • 31
  • 45
Nader-a
  • 113
  • 6
0

you can try below code to print all the files under the directory and sub directories.

   for root, dir, files in os.walk("/home/cd_files"):
        for file in files:
            print file
LOrD_ARaGOrN
  • 3,884
  • 3
  • 27
  • 49
0
import os
files = os.listdir('your_directory/')
for file in files:
    print(file)
Deekshith MR
  • 73
  • 3
  • 10