list file content into text file with pyrhon

problem:
You need to add into a text file the name of all files in a folder, usually you can use the following command sudo ls *.jpg > stills.txt but you get an error:
"unable to execute /bin/ls: Argument list too long"
this is because the sentence is too long because linux can not hold all content in memory if the folder have an huge amount of data.

solution:
You can use python as a workarund, use the following code to create a text file fith the name of all files in a folder or directory.

import os
from os import listdir
from os.path import isfile, join
#folder that hold the files, in my case 70.000,00
dirName = '/media/USB128/SHARED/RingBell/pictures/'
#crete and array
fileNames=[f for f in listdir(dirName) if isfile(join(dirName, f))]
#empty the file is it was previouly filled
open('file.txt', 'w').close()
#loop and write line by ñine each filename
for file in os.listdir(dirName):
f = open("./stills.txt", "a+")
f.write("/media/USB128/SHARED/RingBell/pictures/"+file+"\n")
f.flush() 
f.seek(0)
f.close()