1

I have a short Python script that opens a directory and puts all of the file names into a .txt file. I have tried a few ways to add a new line after each file name but cannot do it. I also want to convert the entire string to uppercase.

Here is what I have:

import os

#Path where the photos are stored
path1 = r"V:\DATABASES\0 Suspension\Suspensia Pictures"

#Variable to list all the files in the dorectory
file_dir = os.listdir(path1)

#Opens a new text file called Pics
newfile = open('Pics.txt','w')

#Writes lines in the file as a string
newfile.write(str(file_dir))

#Prints out all the file names
#print(file_dir)ode here

What I was thinking for the new line was to add print('\n') after the newfile.write(str(file_dir)) line. However, that did not work.

As for the uppercase I am not sure where to put the .upper().

This is the current output:

X01BJ0041, X01BJ0050, X01BJ0058, 
X01BJ0059, X01BJ0060,X01BJ0061,X01BJ0065

The output I am looking for:

X01BJ0006.JPG
X01BJ0007.JPG
X01BJ0008.JPG
X01BJ0026.JPG
X01BJ0036.JPG
X01BJ0037.JPG
X01BJ0038.JPG
X01BJ0039.JPG
X01BJ0040.JPG
X01BJ0041.JPG
X01BJ0050.JPG
X01BJ0058.JPG
X01BJ0059.JPG

Thanks For All The Help!

Edward Wynman
  • 363
  • 1
  • 10
  • 1
    `print` prints to the console. `newfile.write('\n')` on the other hand would send that line feed character to the file, where you want it to go. – JNevill Jul 01 '22 at 13:21
  • or to expand on what JNevill said, just change the existing line to `newfile.write(str(file_dir)+"\n")` – nigh_anxiety Jul 01 '22 at 13:22
  • As for the upper case requirement, `.upper()` is a method of the string object. So `newfile.write(str(file_dir).upper())` should do the trick. Putting it all together: `newfile.write(str(file_dir).upper() + "\n")` – JNevill Jul 01 '22 at 13:23
  • Ive tried that and i still get a giant list (`['Thumbs.db', 'X01BJ0004', 'X01BJ0026', 'X01BJ0026.JPG', ....]`) and I'm not sure why its a list – Edward Wynman Jul 01 '22 at 13:23
  • "and I'm not sure why its a list" What do you think it should be instead? Why? You have one variable that represents several file names that were read from the operating system, right? How else would it do that? – Karl Knechtel Jul 01 '22 at 20:16

3 Answers3

3

os.listdir() gives you a list of strings, so converting its output would give you a string that looks like a list of strings.

Do this instead:

#Writes lines in the file as a string
newfile.write('\n'.join(file_name.upper() for file_name in file_dir))
navneethc
  • 1,234
  • 8
  • 17
3

Expanding a bit on the other answers, we can use the file.writelines() operation instead of join. Arguably, this makes the corresponding line of code easier to read and allows you to use a list comprehension, but any answer works fine, really.

So the full script would be :

import os

MY_DIR = r"V:\DATABASES\0 Suspension\Suspensia Pictures"
OUTPUT_FILENAME = "Pics.txt"

filenames = os.listdir(MY_DIR)
with open(OUTPUT_FILENAME, "w") as newfile:
    newfile.writelines(name.upper() + "\n" for name in filenames) # As @Daniel Walker noticed, the [] are not necessary for the list comprehension.

Remark file.writelines() is a misnomer as it doesn't implicitly add line separators. You could create you own writelines() function that fixes this but I guess this is a bit overkill for the current subject. See this other StackOverflow thread for more information on the subject.

SpaceBurger
  • 537
  • 2
  • 12
  • 1
    Strictly speaking, that's a generator expression. It's like a lazy version of a list comprehension; uses less memory, but you can only iterate over (loop through) it once. – wizzwizz4 Jul 05 '22 at 09:55
2

os.listdir is doing to give you back a list so, when you call newfile.write(str(file_dir)), it's going to print Python's representation of the list.

You should be writing the elements of file_dir to the file and not file_dir itself. There are two ways to do this. First, you could use a for loop:

for filename in file_dir:
   newfile.write(filename.upper() + '\n')

The one downside of this, though you might not care, is you'll be left with a blank line at the end of your file. This is because the last file name written will have a '\n' at the end of it.

In my opinion, a better solution would be

newfile.write('\n'.join(name.upper() for name in file_dir))

This will take all of your file names and join them together with '\n's between them. That way, you won't get a blank line at the end.

On a side note, you should get in the habit of using with blocks (more commonly known as "context blocks") to manage your file handles:

with open('Pics.txt','w') as newfile:
    newfile.write('\n'.join(name.upper() for name in file_dir))

This way, newfile will be guaranteed to be closed when you leave the block.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45