Here is what I have so far in Windows:
import os
os.chdir("C:/Users/any/Desktop/test")
for files in os.listdir("."):
print files
Now it prints this:
test picture.jpg
test script.bat
test text.txt
But now where I am stuck is the output is going to be random with each folder done so my idea of a solution is to have it take the list and label each one individually as
filename1
filename2
filename3
So now filename1 = test picture.jpg
Edit
Well what I am trying to do is later in my code each filename will be used for example say I was trying to rename my files so that any of those files that contained the letter ‘e’ in it, it would be changed to an ‘a’ character:
import os
os.chdir("C:/Users/any/Desktop/test")
for files in os.listdir("."):
print files
files = files.replace('e', 'a')
print files
But I need to be able to have it do each filename individually so the code could look something like this:
import os
os.chdir("C:/Users/any/Desktop/test")
for files in os.listdir("."):
print filename1
filename1 = filename1.replace('e', 'a')
print filename1
While I am not 100% sure I understand what you are trying to do, you could just try this:
then each index, starting with zero, of
dirlistwould yield an entry from your directory. The index values would in effect take the place of the numbers for your filenames.So rather than having
you’d have
with minimal effort, and could easily refer to individual entries in any order with the index.
And as an added bonus, you could easily iterate through this list of names with a
for-loop if need bewhich would be a bit more tricky with the individual filenames you mentioned in your original post.
Update:
Given your edit to your post, you would have been able to achieve your goal with pretty much your original code: