List all file Name of a directory to Text File

Ricky Rock

I've my code like this, but the problem is it only gives the last file name in output.txt instead of all 30 file names.

import os
path="path I wanted"  
dirList=os.listdir(path)
for filename in dirList:
    print (filename)
    f = open("output1.txt", "w")
    f.write(filename) 
    f.close()
jme

The 'w' flag to 'open' truncates the file on every iteration through the loop. If the file already exists, use the 'a' flag instead. For more info, see here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

List file information in a text file for all the files in a directory

From Dev

Inno Setup: List all file names in an directory

From Dev

Making a text file of list of directory entries in bin

From Dev

Move a list of files(in a text file) to a directory?

From Dev

Delete all files in a directory whose name do not match a line in a file list

From Dev

A text file list for each archive with relative name

From Dev

List file names from a directory and place output in a text file

From Dev

Filter directory list text file based on the short common root directory

From Dev

Path, directory and/or file name

From Dev

List all directories that do NOT have a file with a given file name inside

From Dev

php: name of the directory + name of the file

From Dev

Change all file name and extension in directory in linux or windows

From Dev

Select all files from directory that contain file with given name

From Dev

Select all files from directory that contain file with given name

From Dev

Running functions on all files in a directory and extracting date from name file

From Dev

Recursively search a pattern/text only in the specified file name of a directory?

From Dev

Copy files from a directory if name present in a text file

From Dev

Replacing a text file with a directory name using sed command

From Dev

How to get list of all files from a directory (including its sub-directories) whose file name contains a specific string

From Dev

create new file listing all text files in a directory with perl

From Dev

How to write out all directory names into a text file

From Dev

How to make a text file in every subdirectory so that the text file contains name of the directory

From Dev

list the multiple file in directory

From Dev

list: No such file or directory

From Dev

Command to list all file types and their average size in a directory

From Dev

Read in all the files in a particular directory displays the file names in a html list

From Dev

Command to list all file types and their average size in a directory

From Dev

How to list all files in named directory, file type included?

From Dev

How to recursively list all files of desired file type in a specified directory?

Related Related

  1. 1

    List file information in a text file for all the files in a directory

  2. 2

    Inno Setup: List all file names in an directory

  3. 3

    Making a text file of list of directory entries in bin

  4. 4

    Move a list of files(in a text file) to a directory?

  5. 5

    Delete all files in a directory whose name do not match a line in a file list

  6. 6

    A text file list for each archive with relative name

  7. 7

    List file names from a directory and place output in a text file

  8. 8

    Filter directory list text file based on the short common root directory

  9. 9

    Path, directory and/or file name

  10. 10

    List all directories that do NOT have a file with a given file name inside

  11. 11

    php: name of the directory + name of the file

  12. 12

    Change all file name and extension in directory in linux or windows

  13. 13

    Select all files from directory that contain file with given name

  14. 14

    Select all files from directory that contain file with given name

  15. 15

    Running functions on all files in a directory and extracting date from name file

  16. 16

    Recursively search a pattern/text only in the specified file name of a directory?

  17. 17

    Copy files from a directory if name present in a text file

  18. 18

    Replacing a text file with a directory name using sed command

  19. 19

    How to get list of all files from a directory (including its sub-directories) whose file name contains a specific string

  20. 20

    create new file listing all text files in a directory with perl

  21. 21

    How to write out all directory names into a text file

  22. 22

    How to make a text file in every subdirectory so that the text file contains name of the directory

  23. 23

    list the multiple file in directory

  24. 24

    list: No such file or directory

  25. 25

    Command to list all file types and their average size in a directory

  26. 26

    Read in all the files in a particular directory displays the file names in a html list

  27. 27

    Command to list all file types and their average size in a directory

  28. 28

    How to list all files in named directory, file type included?

  29. 29

    How to recursively list all files of desired file type in a specified directory?

HotTag

Archive