Reputation: 275
I've just been asked to come up with a script to find files with a certain filename length. I've decided to try out Python for the first time for this task as I've always wanted to learn it.
I've got the script to find the files and append them to a text file but it does not write a line break for each new entry. Is there a way to do this as the current output is almost unreadable?
Upvotes: 2
Views: 4879
Reputation: 342333
I am wondering why you don't have a line break when you print. an example.
>>> import os
>>> for files in os.listdir("."):
... if os.path.isfile(files):
... print "file: ",files," length: ",len(files)
...
file: test1 length: 5
file: shell.sh length: 8
file: test.txt length: 8
Upvotes: 0
Reputation: 881575
You just need to explicitly append a '\n'
each time you want a line break -- if you're appending to the output file in text mode, this will expand to the proper line separation where needed (e.g. Windows). (You could use os.linesep instead, if you had to output in binary mode for some reason, but that's a pretty unusual use case -- normally, text mode and \n
are much better).
Upvotes: 10