0

I am fetching file names from my local file directory and there are 411 items there but after printing it shows me file names and I am not able to count them in python and second problem is by saving in CSV format, it doesn't show me all the file names? Where is my mistake?

import os

FOLDER_PATH = '/home/bilal/Documents/Books/English/'

def listDir(dir):
    fileNames = os.listdir(dir)
    for fileName in fileNames:
        my_file = open('my_file.csv', 'w')
        my_file.write(fileName)
        # print('File Name: ' + fileName)


if __name__ == '__main__':
    listDir(FOLDER_PATH)
AMC
  • 2,642
  • 7
  • 13
  • 35
  • Ok, you have shown your attempt. But here we also need the actual and expected result to be able to help you :-) – Serge Ballesta Mar 25 '20 at 16:51
  • Does this answer your question? [How do I list all files of a directory?](https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory) – Quantum Dreamer Mar 25 '20 at 16:52
  • As an aside, variable and function names should generally follow the `lower_case_with_underscores` style. – AMC Mar 25 '20 at 16:55
  • I am trying to fetch all the filenames not pathnames. – Red Mailers Mar 25 '20 at 16:59
  • It would even be more clear of an example of what you have and what you want... I shall not answer without that. Not that I do not want, but without it I cannot be sure if my answer will address your problem. – Serge Ballesta Mar 25 '20 at 17:10
  • I edited my question. Please read now. – Red Mailers Mar 25 '20 at 17:11
  • _I am fetching file names from my local file directory and there are 411 items there but after printing it shows me file names and I am not able to count them_ What do you mean? How are you not able to count them? _by saving in CSV format, it doesn't show me all the file names?_ Nothing to do with csv, you aren't even doing anything csv-specific. The file mode `"w"` deletes the contents of the file before writing. – AMC Jun 09 '20 at 23:40

1 Answers1

-1
import glob
path = r"/home/bilal/Documents/Books/English/*.csv"
my_file = open('my_file.csv', 'w')
fileList = list(map(os.path.basename, glob.glob(path)))
for filename in fileList:
    print(filename)
    my_file.write(filename)
my_file.close()

or

import glob
path = r"/home/bilal/Documents/Books/English/*.csv"
with open('my_file.csv', 'w') as my_file:
    fileList = list(map(os.path.basename, glob.glob(path)))
    for filename in fileList:
        print(filename)
        my_file.write(filename)
Quantum Dreamer
  • 432
  • 1
  • 6
  • 17
  • I am not trying to find the pathnames. I am trying to write the filename. Didn't you read the question? – Red Mailers Mar 25 '20 at 16:57
  • @Red Mailers Your question says "How to complete number of file names" for that you can use length of list returned. Your code implies that you are trying to print each file name. Please be more specific Edited my answer anyway – Quantum Dreamer Mar 25 '20 at 17:03
  • Your edition didn't give me answer. I am now specific in my question. I edited it. Please read now. I want it to be written all the filenames in csv format. – Red Mailers Mar 25 '20 at 17:10
  • So you want to write all the filenames to a csv file? If you iterating everytime You should open the file as 'a' . If you open the file with an as 'w' everytime your existing myfile.csv is truncated – Quantum Dreamer Mar 25 '20 at 17:31
  • You didn't answer my question. It is not working. Although you understood. – Red Mailers Mar 25 '20 at 18:30
  • While this may answer the question, it was flagged for review. Answers with no explanation are often considered low-quality. Please provide some commentary in the answer for why this is the correct answer. – Dan Mar 26 '20 at 03:35
  • Updated the answer. – Quantum Dreamer Mar 26 '20 at 14:45