-1

i have an listing that read files from a given directory, and then prints it out to console, and now i am wanting to output that list of files to a file, and i got that working except it puts it all on one line, how can i add a new line? I have tried \n but it dose nothing. I know im using it incorrectly. here is my code block.

        for path in files:
            print(path)

            # Open the file for writing
            file = open('looking-for.txt', "w", encoding="utf-8")
            # grab the list for writing
            content = str(files)
            # write out the header to the file.
            file.write("----------------------------------\n")
            file.write("| Looking for files              |\n")
            file.write("----------------------------------\n")
            # write out the list to a file
            file.write(content)
            # close the file.
            file.close()

heres what i have tried.

this dose not give me a new line.
file.write(string)
file.write("\n")
here is what I get.
drive:\path\to\file1, drive:\path\to\file2, drive:\path\to\file3
etc.
here is what I am trying to achieve.
drive:\path\to\file1
drive:\path\to\file2
drive:\path\to\file3
etc.
whitevamp
  • 3
  • 2
  • 1
    Does this answer your question? [Writelines writes lines without newline, Just fills the file](https://stackoverflow.com/questions/13730107/writelines-writes-lines-without-newline-just-fills-the-file) – JonSG Aug 01 '23 at 19:44
  • Have you tried https://stackoverflow.com/questions/21839803/how-to-append-new-data-onto-a-new-line? – Uberhumus Aug 01 '23 at 20:41
  • `file.write(content + '\n')` would do the trick. – D.L Aug 01 '23 at 20:58
  • file.write(content + '\n') <-- puts them all on one line. and I have tried file.writelines(f'{s}\n' for s in content) <-- gives me one letter per line file.write(''.join(content), '\n')<-- gives me one letter per line – whitevamp Aug 01 '23 at 21:52

1 Answers1

0

First, i think you lied when you claimed your output was

drive:\path\to\file1, drive:\path\to\file2, drive:\path\to\file3

I think your actual output is

['drive:\path\to\file1', 'drive:\path\to\file2', 'drive:\path\to\file3']

which is precisely the string formatting of a list, which is what you are constructing here:

# grab the list for writing
content = str(files)

where the comment is also completely wrong. You aren't "grabbing" the list, you are reformatting the entire list of files into a format you don't actually want at all.

Additionally, you are re-doing this inside the loop, for each path, re-write the entire output with the header and the entire faulty file listing. What? Clearly not. You want to only open the file once, write the header only once, then write each file.

All in all:

with open ('looking-for.txt', "w", encoding="utf-8") as file:
    file.write("----------------------------------\n")
    file.write("| Looking for files              |\n")
    file.write("----------------------------------\n")

    for path in files:
        file.write(path + '\n')

    # Alternatively write all files at once by formatting
    # the content the way we *actually* want it (joined by new-lines)
    # content = '\n'.join(files)
    # file.write(content)
Mikael Öhman
  • 2,294
  • 15
  • 21