Python -Listendateien im aktuellen Verzeichnis
import os
files = os.listdir('.')
print(files)
for file in files:
# do something
Soggy Boi
import os
files = os.listdir('.')
print(files)
for file in files:
# do something
import os
files_and_directories = os.listdir("path/to/directory")
import os
print(os.listdir('/path/to/folder/to/list'))
import os
def fn(): # 1.Get file names from directory
file_list=os.listdir(r"C:\Users")
print (file_list)
#2.To rename files
fn()
import os
lst=os.listdir(directory)
import os
# returns name of all files & directory exist in current location
files_dir = os.listdir('./blogs')
# './blogs' is the directory in the current location
print(files_dir)
only_files = []
for i in files_dir:
if os.path.isfile('./blogs/'+i):
only_files.append(i)
only_dir = []
for i in files_dir:
if os.path.isdir('./blogs/'+i):
only_dir.append(i)
print('-'*15)
print(only_files) # prints all files
print('-'*15)
print(only_dir) # prints all directories
"""
OUTPUT:
['1.txt', '2.txt', '3.txt', '4.txt', '5.txt', '6.txt', '7.txt', '8.txt', 'Test Directory 1', 'Test Directory 2']
---------------
['1.txt', '2.txt', '3.txt', '4.txt', '5.txt', '6.txt', '7.txt', '8.txt']
---------------
['Test Directory 1', 'Test Directory 2']
"""