Why only one name could be accepted

def path(request, mypath):
    mypath = request.path_info
    _listdir = os.listdir(mypath)  # ['folder1', 'folder2', 'folder3', 'folder4']
    mess = _listdir
    a = ' '
    x=0
    scope = vars()  

    for i in mess:  
        scope['x']+=1  
        a += mess[x]
        a += '\n'

    return HttpResponse(a)

I hope the output will be like this:

folder1
folder2
folder3
folder4

but why does the output look like this:

folder1
folder1
folder1
folder1

any help?

0
source share
5 answers
I hope the output is like this:

folder1
folder2
folder3
folder4

So you will have your way out ...

for i in os.listdir(mypath):
    print i

You can return iin a loop with HttpResponseno problem, do it

returnString = ""
for i in os.listdir(mypath):
    returnString = returnString + i + "\n"

return returnString
+1
source

There are huge pieces of unnecessary code in this function.

def path(request):
    return HttpResponse('\n'.join(os.listdir(request.path_info)))

The task is completed!

+4
source

:

. : undefined.

, .

+3

,

a += mess[i]

a += mess[x]
+1

Most of what you have does not matter. You just want to iterate over the return values. Do not modify them or play with the variable indirectly through scope.

def path(request, mypath):
    mypath = request.path_info
    dirs = os.listdir(mypath)  # ['folder1', 'folder2', 'folder3', 'folder4']
    a = ''

    for i in dirs:  
        a += dirs
        a += '\n'

    return HttpResponse(a)
+1
source

Source: https://habr.com/ru/post/1774440/


All Articles