How to read file's name in current folder?

I got his here but don’t get the full file’s name only the type of file

import bpy
import os,sys

import glob

contents = os.listdir(’.’) #contents of the current directory
files = []
directory = []

for i in contents:
if os.path.isfile(i) == True :
files.append(i)
elif os.path.isdir(i) == True :
directory.append(i)

print (‘files =’,files)
print (‘directory =’,directory)

get a list of current folder name + the file extension

file = readfile-folders1.py
file = readfileinfolders1.blend
file = readfileinfolders1.blend1
files = [‘readfile-folders1.py’, ‘readfileinfolders1.blend’, ‘readfileinfolders1.blend1’]
directory = []

so how can I get the whole file’s name

thanks
happy bl

Do you mean you need full names including paths? os.path.abspath() works for that:

import os

files = []
directories = []
for i in os.listdir():
    if os.path.isdir(i):
        directories.append(os.path.abspath(i))
    elif os.path.isfile(i):
        files.append(os.path.abspath(i))
    
print('directories = ', directories)
print('files = ', files)

is there a way to remove the path ?

and would it be possible to list files per directories if there are sub folders

I found a few sites for listing files name but
could not get the full name only the extension
not certain why !

thanks
happy bl

import os
path = "C:\\Blender\\2.79\scripts\\addons_contrib\\presets"
print()
for (dirpath, dirnames, filenames) in os.walk(path):
	print ("dirpath:", dirpath)
	print ("filenames:",filenames)
1 Like

@Juhaw
that is giving the full file’s name

but would like to understand why the other method does not give the full name !
very strange!

I will try to modify it to do all sub folder with their own files
later on this night

thanks for feedback

happy bl

Filenames are without filepath.

I was talking about the first example
and I saw others examples too and they give the path + the file’s extension and not the whole file name with extension
which is very strange the doc seems to indicate that it should give full filename
but I don’t get it
may be because it is not the same python lib that blender use !

thanks
happy bl

os.walk will do just that on it’s own.

did you test your first example ?
I was not able to get the full file name
just the current folder with the file’s extension!

see my respons for this

thanks
happy bl

Please try to be more specific and illustrate what you mean with screenshots, console output and examples. The words ‘full file name’ may refer to the file name with or without the path. Something like ‘C:\SomeDirectory\someFile.txt’ is considered to be a ‘full file name’, however sometimes ‘full file name’ may reffer to only ‘someFile.txt’. Both meanings are valid and depend on the context…

This code:

import os

files = []
directories = []
for i in os.listdir():
    if os.path.isdir(i):
        directories.append(os.path.abspath(i))
    elif os.path.isfile(i):
        files.append(os.path.abspath(i))      
print('\n\n\nDirectories:\n ' + '\n '.join(directories))
print('\nFiles:\n ' + '\n '.join(files))

Gives me this:

Directories:
 C:\TestDirectory\AnEmptyDirectory
 C:\TestDirectory\DirectoryWithSomeWeirdCharctersInItsName-ąčęėįšųūž
 C:\TestDirectory\OtherDirectory
 C:\TestDirectory\SomeDirectory

Files:
 C:\TestDirectory\SomeFileInRootDirectory.txt
 C:\TestDirectory\SomeOtherFile.zip
 C:\TestDirectory\TheBlendFile.blend
 C:\TestDirectory\TheBlendFile.blend1

And then with os.walk you can walk the directory tree so this code:

import os
files=[]
directories=[]
for root, dirs, filenames in os.walk(os.getcwd()):
    for every_file in filenames:
        files.append(os.path.join(root, every_file))
    for every_dir in dirs:
        directories.append(os.path.join(root, every_dir))
    
print('\n\n\nDirectories:\n ' + '\n '.join(directories))
print('\nFiles:\n ' + '\n '.join(files))

gives me this:

Directories:
 C:\TestDirectory\AnEmptyDirectory
 C:\TestDirectory\DirectoryWithSomeWeirdCharctersInItsName-ąčęėįšųūž
 C:\TestDirectory\OtherDirectory
 C:\TestDirectory\SomeDirectory

Files:
 C:\TestDirectory\SomeFileInRootDirectory.txt
 C:\TestDirectory\SomeOtherFile.zip
 C:\TestDirectory\TheBlendFile.blend
 C:\TestDirectory\TheBlendFile.blend1
 C:\TestDirectory\DirectoryWithSomeWeirdCharctersInItsName-ąčęėįšųūž\SomeTextFile.txt
 C:\TestDirectory\OtherDirectory\SomeImage - Copy.jpg
 C:\TestDirectory\OtherDirectory\SomeImage.jpg
 C:\TestDirectory\SomeDirectory\1.jpg
 C:\TestDirectory\SomeDirectory\2.jpg
 C:\TestDirectory\SomeDirectory\3.jpg

not certain why it did not work yesterday
but today looks like most of the algo seem to work !

will re test your latest examples.

I just did a test with a simple Walk algo
and it looks like it is very simple and works nicely

I think I will us that one to list all the blender files

is there a simple way to add a filter for instance if you want to get only the Blend file from all the folders

thanks
happy bl

got this one which is close to the list I want
just have to save this in a txt file

import bpy

import os

fp = bpy.data.filepath
path1 = os.path.dirname(fp)
print ('path1 = ', path1 )
print ()

for (dirpath, dirnames, filenames) in os.walk(path):

print ("dirpath:", dirpath)

jjf1 = 1

for f1 in filenames:

	extension = os.path.splitext(f1)
	filename, file_extension = os.path.splitext(f1)

print ('extension = ', file_extension)

print (‘len =’, len (file_extension))

	if file_extension == '.blend':
		print ('files ', jjf1 , '= ',f1)

		jjf1+=1
	
print ()

print ()

thanks
happy bl